0

我需要为 Trustpilot 小部件的客户生成唯一链接。为此,我需要一个访问令牌。我尝试了所有可能的方法,但面临同样的错误

{“原因”:“未知的授权类型”}“

下面是我的相同代码

$data = array(
GRANT_TYPE => GRANT_TYPE_VALUE,
TRUSTPILOT_USERNAME => TRUSTPILOT_USERNAME_VALUE,
TRUSTPILOT_PASSWORD => TRUSTPILOT_PASSWORD_VALUE,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,“https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken”);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
‘Authorization: Basic base64 encoded apikey:secretkey’,
‘Content-Type: application/json’
));
$server_output = curl_exec($ch);
curl_close($ch);
var_dump($server_output);

提前致谢

4

2 回答 2

2

我让它工作了,但我必须做一些事情才能让它工作。

有效载荷数据必须http_build_query像这样转换

$data = http_build_query(array(
  'grant_type' => 'password',
  'username' => $email,
  'password' => $password,
));

然后 curl 在从服务器验证 SSL 证书时遇到问题。所以我添加了这两行:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

标题值更改为

$authorization = 'Basic '. base64_encode($key . ':' . $secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: ' . $authorization,
  'Content-Type: application/x-www-form-urlencoded'
 ));

最后的代码片段:

$data = http_build_query(array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
));
$authorization = 'Basic '. base64_encode($key . ':' . $secret);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: ' . $authorization,
  'Content-Type: application/x-www-form-urlencoded'
 ));

希望这会帮助你。

于 2017-10-12T09:58:14.683 回答
0

你试过设置Content-Typeapplication/x-www-form-urlencoded

于 2017-10-11T20:39:19.260 回答