0

完整的代码。

    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 请求失败!/home 中的 HTTP/1.0 400 错误请求.....

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

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

如何修复该错误?

4

1 回答 1

1

您可能想要使用服务器端身份验证流程。通过检查文档中的调用很清楚,您的哪些调用是错误的。

首先,您对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'];
于 2012-08-27T15:18:33.170 回答