我正在尝试使用授权代码获取访问令牌、刷新令牌等。我使用以下代码,但它会产生以下错误:invalid Auth code
。我正在使用客户端ID和机密ID的沙箱凭据。
我从iOs设备接收$PaypalToken
那么问题出在哪里呢?
这是贝宝的原始请求:
卷曲https://api.paypal.com/v1/oauth2/token'\
-H"内容类型:应用程序/x-www-form-urlencoded"\
-H"授权:基本QWZV…=="\
-d'grant_type=authorization_code&response_type=令牌&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code=EBYhRW3ncivudQn8UopLp4A28…'
PHP Code:
$host2 = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$clientId = 'XXXXXXX';
$clientSecret = 'xxxxxx';
$PaypalToken = $values->Token;
$postdata = 'grant_type=authorization_code&response_type=token&redirect_uri=urn:ietf:wg:oauth:2.0:oob&code='.$PaypalToken;
//$postdata = 'grant_type=client_credentials&response_type=token&code=wwwwwwwwwwwww';
//$postdata = 'grant_type=client_credentials';
$curl = curl_init($host2);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_USERPWD, $clientId.":".$clientSecret);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
# curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
$response = curl_exec( $curl );
// Convert the result from JSON format to a PHP array
$json_response = json_decode($response,true);
if (empty($response)) {
curl_close($curl); // close cURL handler
$returnArr['Success'] = 0;
$returnArr['Message'] = $json_response['error_description'];
echo json_encode($returnArr);exit;
} else {
$info = curl_getinfo($curl);
//echo "<pre>";print_r($info);print_r($response);
curl_close($curl);
if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
$returnArr['Success'] = 0;
$returnArr['Message'] = $json_response['error_description'];
echo json_encode($returnArr);exit;
}
}
echo "<pre>";print_r($json_response);
如果我更改$postdata = 'grant_type=client_credentials';
,则返回以下内容:
阵列([scope]=>openidhttps://uri.paypal.com/services/invoicinghttps://api.paypal.com/v1/payments/.*https://api.paypal.com/v1/vault/credit-cardhttps://api.paypal.com/v1/vault/credit-card/.*
[access_token]=>A015Kroyqmm6eJqEzxX65jtGsd-L96g20KsAugrvSkB
[token_type]=>承载器
[app_id]=>app-80W284485P519543T
[expires_in]=>28800
)
那么如何获取aceeess令牌、刷新令牌呢?提前谢谢。
我认为这不是你的问题。
您的代码完全正确。
请您的移动应用程序开发人员将Paypal环境设置为Sandbox。它可能在NoNetwork上。
因为我已经面对过一次这个问题。
祝你好运。让我知道你的答案。
谨致问候。