paypal access oauth登录json_decode返回null

时间:2022-10-14 16:46:52

i have 2 PHP files, one index.php and the other one paypal.php. The code for paypal.php is:

我有两个PHP文件,一个索引。php和另一个paypal.php。贝宝的代码。php是:

    <?php 

session_start();


$client_id = 'xxxxxxxxxxxx';            
$client_secret = 'xxxxxxxxxxxxxxxxxxxx';    
$scopes = 'email profile';                    
$app_return_url = 'http://xxx.com/xxx/paypal.php';  
$nonce = time() . rand();

$code = $_REQUEST["code"];

if(empty($code)) {

    $_SESSION['state'] = md5(uniqid(rand(), TRUE)); 
    $paypal_auth_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/authorize?"
            ."client_id=".$client_id
            ."&response_type=code"
            ."&scope=".$scopes
            ."&nonce=".$nonce
            ."&state=".$_SESSION['state']
            ."&redirect_uri=".urlencode($app_return_url);

    header("Location: $paypal_auth_url");     
}else{

    $token_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/tokenservice";    
    $postvals = "client_id=".$client_id
            ."&client_secret=".$client_secret
            ."&grant_type=authorization_code"
            ."&code=".$code;


    $ch = curl_init($token_url);
    $options = array(
                CURLOPT_POST => 1,
                CURLOPT_VERBOSE => 1,
                CURLOPT_POSTFIELDS => $postvals,
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_SSLVERSION => 3
    );
    curl_setopt_array( $ch, $options );
    $response = curl_exec($ch);
    curl_close($ch);
    $atoken = json_decode($response);


    $profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?"
            ."schema=openid"
            ."access_token=".$atoken->access_token;

    $ch = curl_init($profile_url);
    $options = array(
                CURLOPT_RETURNTRANSFER => 1,
                CURLOPT_SSLVERSION => 3
    );
    curl_setopt_array( $ch, $options );
    $response = curl_exec($ch);
    curl_close($ch);
    $profile= json_decode($response,true);  



    $_SESSION['paypal_user'] = "true";
    $_SESSION['profile'] = $profile;


    echo("<script> top.location.href='index.php'</script>");
}
?>

The code for index.php is:

索引的代码。php是:

    <?php
session_start();

// LOGOUT
if ($_GET['logout'] == 'true'){
    $_SESSION['paypal_user']="";    
}


if (strlen($_SESSION['paypal_user'])){
    // LOGGED USER
    echo "<pre>";
    print_r($_SESSION['profile']);
    echo "</pre>";
    echo "<br><BR> <a href='?logout=true'>LOGOUT</a>";
}else{
    // LOGIN
?>
    <a href='paypal.php' title='Paypal oAuth Login'>
    <img src='https://www.paypalobjects.com/en_US/Marketing/i/btn/login-with-paypal-button.png'>
    </a>
<?
}
?>

Any ideas why this code is not working? I tried var_dump json_decode and it returns null.

你知道为什么这个代码不能工作吗?我尝试了var_dump json_decode,它返回null。

Thank you!

谢谢你!

2 个解决方案

#1


0  

I may be wrong, but I believe you scopes need to be...

我可能错了,但我相信你的范围需要……

  "scopes": "email https://uri.paypal.com/services/paypalattributes",

The URL is the profile

URL是概要文件

#2


0  

simply add '&' on before access token,he missed & symbol between two variables

在访问令牌之前,只需在两个变量之间添加'&',他就会错过&符号

$profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?" ."schema=openid" ."**&**access_token=".$atoken->access_token;

美元profile_url = " https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo? "模式= openid。”* * & * * access_token = " $ atoken - > access_token;

#1


0  

I may be wrong, but I believe you scopes need to be...

我可能错了,但我相信你的范围需要……

  "scopes": "email https://uri.paypal.com/services/paypalattributes",

The URL is the profile

URL是概要文件

#2


0  

simply add '&' on before access token,he missed & symbol between two variables

在访问令牌之前,只需在两个变量之间添加'&',他就会错过&符号

$profile_url = "https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo?" ."schema=openid" ."**&**access_token=".$atoken->access_token;

美元profile_url = " https://www.paypal.com/webapps/auth/protocol/openidconnect/v1/userinfo? "模式= openid。”* * & * * access_token = " $ atoken - > access_token;