Facebook OAuth access_token错误

时间:2012-08-26 21:53:38

标签: facebook facebook-graph-api oauth

完整代码。

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

当我尝试向Facebook进行GET查询时。

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

我收到错误:

  

无法打开流:HTTP请求失败! HTTP / 1.0 400错误请求   在/ home .....

当手动将$ dbpath值(路径)粘贴到Web浏览器时,我收到了下一个错误:

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

如何解决该错误?

1 个答案:

答案 0 :(得分: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'];