【问题标题】:Trustpilot Oauth use token PHPTrustpilot Oauth 使用令牌 PHP
【发布时间】:2019-11-28 06:47:35
【问题描述】:

我使用此脚本在 Trustpilot 中检索 Oauth 令牌

function get_accesstoken($tp_username,$tp_password,$api_key,$api_secret)
{
    $url = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';
    $payloadName = array(
        'grant_type' => 'password',
        'username' => $tp_username,
        'password' => $tp_password
    );
    $payload = http_build_query($payloadName);
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curl, CURLOPT_USERPWD, "$api_key:$api_secret");
    curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    $return = json_decode(curl_exec($curl));
    print_r($return);
    curl_close($curl);
    return $return->access_token;

}

现在,我怎样才能检索令牌,验证是否存在并使用它? 谢谢

【问题讨论】:

    标签: php oauth-2.0 trustpilot


    【解决方案1】:
    $url = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
    $key = "YOUR API KEY";
    $secret = "YOUR API SECRET";
    $username = 'YOUR LOGIN EMAIL';
    $password = 'YOUR LOGIN PASSWORD';
    $payload = array(
                    'grant_type' => 'password',
                    'username' => $username,
                    'password' => $password,
                );
    $authEncodedKeys = base64_encode($key.":".$secret);
    $options = array(
    'http' => array (
      'header' => "Authorization: Basic ".$authEncodedKeys.
                  "Content-Type: application/x-www-form-urlencoded",
      'method' => 'POST',
      'content' => http_build_query($payload)
    )
    );
    $context = stream_context_create($options);
    $results = json_encode(file_get_contents($url, false, $context));
    
    echo $results;
    

    这对我有用。试试看。

    【讨论】:

    • 考虑添加一个简短的介绍,介绍您的代码的作用或问题代码之间的变化。
    猜你喜欢
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 2019-10-17
    • 2016-10-14
    • 2021-09-28
    • 2017-06-24
    • 1970-01-01
    • 2016-12-03
    相关资源
    最近更新 更多