2013-03-26 82 views
0

我有2個PHP文件,一個index.php和另一個paypal.php。 爲paypal.php的代碼是:貝寶訪問oauth登錄json_decode返回null

<?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>"); 
} 
?> 

的代碼的index.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> 
<? 
} 
?> 

爲什麼這個代碼不工作的任何想法?我試過var_dump json_decode,它返回null。

謝謝!

+0

究竟發生了什麼?它在哪裏失敗,你看到了什麼? – 2013-03-26 23:12:36

+0

它將我重定向到paypal訪問登錄頁面,在我登錄後它應該將我重定向到index.php和print_r返回的響應,但它返回null。你可以在這裏找到一個工作演示:http://ngiriraj.com/socialMedia/paypal_oauth/index.php不知何故他分享的代碼是錯誤的 – user2212817 2013-03-26 23:43:55

+0

嘗試在貝寶存儲庫中提供的示例https://github.com/paypal/paypal -access/tree/master/oauth2-php-ppaccess – siddick 2013-03-27 01:56:58

回答

0

我可能是錯的,但我相信你作用域必須...

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

的URL是輪廓