0

I retrieve the Oauth token in Trustpilot with this script

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;

}

now, how can I retrieve the token, verify is exist and use it? thanks

4

1 回答 1

0
$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;

这对我有用。试试看。

于 2019-11-27T22:47:35.130 回答