Facebook:“此授权码已被使用.",“类型":“OAuthException",“代码":100 [英] Facebook: "This authorization code has been used.","type":"OAuthException","code":100

查看:47
本文介绍了Facebook:“此授权码已被使用.",“类型":“OAuthException",“代码":100的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚升级到 PHP 5.4.19 和 facebook-php-sdk-v4.

I just upgraded to PHP 5.4.19 and facebook-php-sdk-v4.

是我一个人还是FB故意让整合变得困难?!例如,我不使用 Composer(无法将其安装在我的共享主机上),因此加载新类需要特定的(为您自己发现)排序——这已经够头疼了!http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/不完全正确.

Is it just me or has FB made the integration deliberately difficult?! For instance, I don't use Composer (can't install it on my shared host) so loading the new classes required a specific (discover-for-yourself) ordering - that was enough headache! The suggested solution at http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/ wasn't completely correct.

无论如何,当我最终让它运行并在应用高级设置选项卡作为 Facebook 推荐 我陷入了困境 22.

Anyway, when I finally got it to run and enabled "App Secret Proof for Server API calls" under the App advanced settings tab as recommended by Facebook I got into a catch 22.

就是这样:

1) 从我的服务器进行 FB API 调用,例如$request = new FacebookRequest($session, 'GET', '/me'); 我现在必须提供一个 appsecret_proof 参数.

1) To make an FB API call from my server, e.g. $request = new FacebookRequest($session, 'GET', '/me'); I must now provide an appsecret_proof argument.

2) 要创建 appsecret_proof,我需要一个 access_token,即 $appsecret_proof= hash_hmac('sha256', $access_token, $app_secret);.

2) To create an appsecret_proof I need an access_token i.e. $appsecret_proof= hash_hmac('sha256', $access_token, $app_secret);.

3) 要获得一个只有 $_GET['code'] 的 access_token,我必须通过以下方式进行代码交换获取 https://graph.facebook.com/oauth/access_token?client_id={app-id}&redirect_uri={redirect-uri}&client_secret={app-secret}&code={code-parameter}.

3) To get an access_token with only $_GET['code'] at this point, I must do code exchange via GET https://graph.facebook.com/oauth/access_token? client_id={app-id} &redirect_uri={redirect-uri} &client_secret={app-secret} &code={code-parameter}.

4) 为了调用 FB 进行代码交换,我收到错误 {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}.

4) To call FB for code exchange I get the error {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}.

因此产生了两个问题:

1) 除了通过代码交换,我还能如何获得 access_token,以便我可以使用该令牌创建 appsecret_proof 并进而调用 FacebookRequest?

1) How else can I get an access_token except via code exchange, so that I can use that token to create an appsecret_proof and in turn call FacebookRequest?

2) 我在哪里/如何将 appsecret_proof 放入 FacebookRequest?也许是这样 $request = new FacebookRequest($session, 'GET', '/me', array("appsecret_proof" => $appsecret_proof));?我似乎找不到有关如何将 appsecret_proof 与 PHP API 一起使用的具体说明(很清楚如何通过 http 与 Graph API 进行操作).

2) Where/How do I put that appsecret_proof into FacebookRequest? Is it perhaps this way $request = new FacebookRequest($session, 'GET', '/me', array("appsecret_proof" => $appsecret_proof));? I cannot seem to find the specific instruction on how to use appsecret_proof with PHP API (it is clear how to do it via http with Graph API).

推荐答案

女士们,先生们,我解决了所有问题 - 我只需要使用 $access_token = $session->getToken();.这帮助我否定了导致 OAuthException 的代码交换调用,因为 Facebook 从那以后改变了他们关于交换代码的政策,不再使用多次.

Ladies and Gentlemen, I resolved it all - I just needed to use $access_token = $session->getToken();. This helped me negate the call for code exchange which was causing OAuthException because Facebook has since changed their policy on the exchange code from being used more than once.

现在按照 Facebook 的建议,在应用高级设置选项卡下正确启用了服务器 API 调用的应用秘密证明".

Now "App Secret Proof for Server API calls" is properly enabled under the App advanced settings tab as recommended by Facebook.

那么具体的解决方案就完整了:

So the specific solution in complete:

$app_id = 'APPID'; $app_secret = 'APPSECRET';
FacebookSession::setDefaultApplication($app_id, $app_secret);
$redirect_url = "https://mydomain.com/login";
$helper = new FacebookRedirectLoginHelper($redirect_url);

try {
    $session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
} catch (Exception $ex) {
}

if (isset($session)) {
    $access_token = $session->getToken();
    $appsecret_proof = hash_hmac('sha256', $access_token, $app_secret);
    $request = new FacebookRequest($session, 'GET', '/me', array("appsecret_proof" =>  $appsecret_proof));
    $response = $request->execute();
    $graphObject = $response->getGraphObject();

   echo print_r($graphObject, 1);
} else {
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>';
}

这篇关于Facebook:“此授权码已被使用.",“类型":“OAuthException",“代码":100的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆