简体   繁体   中英

Invalid Auth code error in paypal

I am trying to get access token, refresh token etc. using authorize code. I am using following code, but it produces this error: invalid Auth code . I am using sandbox credentials for client ID and Secret Id.

I receive $PaypalToken from iOs device.
So what is the problem?

This is the original paypal request:

curl ' https://api.paypal.com/v1/oauth2/token ' \\
-H "Content-Type: application/x-www-form-urlencoded" \\
-H "Authorization: Basic QWZV...==" \\
-d 'grant_type=authorization_code&response_type=token& 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); 

If i change $postdata = 'grant_type=client_credentials'; then it returns follwing:

Array ( [scope] => openid https://uri.paypal.com/services/invoicing https://api.paypal.com/v1/payments/ .* https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/vault/credit-card/ .*
[access_token] => A015KRoybqmm6eJqEzxX65jtGsd-L96g20KsAugrvSkB
[token_type] => Bearer
[app_id] => APP-80W284485P519543T
[expires_in] => 28800
)

So how to get aceeess token, refresh token? Thanks in advance.

I think its not your side issue.

Your code is completely correct.

Ask your mobile application developer to set up the Paypal enviornment to Sandbox. It may be on NoNetwork.

As I have already faced this issue once.

Best of luck. Let me know your answer.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM