2012-08-26 93 views
0

完整代碼。Facebook OAuth access_token錯誤

public function indexAction(){ 
      echo '<a href="https://www.facebook.com/dialog/oauth?client_id=475302972487577&redirect_uri=http://bp.mysite.com/en/social/fblogin" target="_blank">Login met facebook</a> '; 
    } 

const FB_GRAPH_URL = "https://graph.facebook.com/"; 
    public function fbloginAction() { 

      $fbCode = $this->_getParam("code"); 
      $getStr = self::FB_GRAPH_URL. 'oauth/access_token?' .http_build_query(array(
         'client_id'  => 'APP_ID', 
         'type'   => 'client_cred', 
         'client_secret' => 'SECRET_KEY', 
         'code'   => $fbCode) 
        ); 

      $accessToken = file_get_contents($getStr); 
      krumo($accessToken) ; 

      $dbpath = "https://graph.facebook.com/me?$accessToken" ; 
      $cont = file_get_contents($dbpath) ; 
      krumo($cont); 
     } 

當我嘗試讓GET查詢到Facebook。

$dbpath = "https://graph.facebook.com/me?$accessToken" ; 
$cont = file_get_contents($dbpath) ; 

我收到的錯誤:

failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home.....

當粘貼手動$ DBPATH值(路徑)的網頁瀏覽器,我得到了一個錯誤:

{ 
    "error": { 
     "message": "An active access token must be used to query information about the current user.", 
     "type": "OAuthException", 
     "code": 2500 
    } 
} 

如何解決這個錯誤?

+1

如果你沒有足夠的閱讀,理解和實現oauth2,爲什麼你不使用[Facebook的官方PHP SDK](https://github.com/facebook/facebook-php-sdk)? – zerkms

+0

先生,我不能站在如何使用它。這個SDK的例子不適用於我的網站。在哪裏可以找到「一步一步」手動使用Facebook API? –

+1

1. http://developers.facebook.com/ 2.有幾個例子(https://github.com/facebook/facebook-php-sdk/tree/master/examples)附帶fb php sdk – zerkms

回答

1

您可能想要使用服務器端身份驗證流程。通過檢查documentation中的呼叫,很明顯,您的哪些呼叫是錯誤的。

首先,你對oauth/access_token終點呼叫沒有參數'type' => 'client_cred',但它需要再次爲您redirect_uri參數:

$getStr = self::FB_GRAPH_URL . 'oauth/access_token?' . http_build_query(array(
      'client_id'  => 'APP_ID', 
      'redirect_uri' => 'REDIRECT_URI', 
      'client_secret' => 'SECRET_KEY', 
      'code'   => $fbCode) 
     ); 

然後,你不能只是藉此呼籲的答案你access_token ,因爲在它更:

access_token=USER_ACCESS_TOKEN&expires=NUMBER_OF_SECONDS_UNTIL_TOKEN_EXPIRES 

,你只希望它的access_token部分:

$response = file_get_contents($getStr); 
$params = null; 
parse_str($response, $params); 

$dbpath = "https://graph.facebook.com/me?access_token=" . $params['access_token']; 
+0

謝謝,你幫了我很多! :) –

相關問題