【问题标题】:Intermittent bad Facebook app access_token间歇性坏 Facebook 应用程序 access_token
【发布时间】:2012-01-11 15:24:51
【问题描述】:

我正在 Facebook 中为我的应用程序创建测试用户。它有时会起作用。如果没有,则此调用会发生错误:

function getTestAccounts($fb, $a) {
$s = urlencode($a);
**$accounts = $fb->api("/{$fb->getAppId()}/accounts/test-users?access_token=$s");**
if( isset($accounts['data']) )
    return $accounts;
else
    return null;
}

错误是:

未捕获的 OAuthException:(#15) 必须使用应用程序 access_token 调用此方法。

之前我通过这个函数获取了token:

function getAppAccessToken($fb) {
$access_token_url = "https://graph.facebook.com/oauth/access_token";
$parameters = "grant_type=client_credentials&client_id=" . $fb->getAppId() .    "&client_secret=" . $fb->getApiSecret() . "&junk=1";
return file_get_contents($access_token_url . "?" . $parameters);
}

当我输出有问题的令牌时,它看起来像这样并且不会改变(应该吗?)

access_token=229234510434203|TK2UDoGCPthCBeDIvhRIPkSG8Wk

我已尝试清除 cookie。这似乎可行,但可能与其他事情巧合,因为现在不行。

我假设从 FB 返回的访问令牌发生了变化,但它总是返回相同的。

【问题讨论】:

  • 如果用户在获取文件时出错,使用 file_get_contents 会暴露您的应用程序机密。不错的大安全漏洞,你应该使用 cURL,

标签: facebook oauth access-token


【解决方案1】:

标记

对于用户访问令牌,我使用以下内容,其中数字是我的应用程序 ID。 "与 php-sdk 一起使用"

$access_token = $_SESSION['fb_135669679827333_access_token'];

对于应用程序访问令牌,我使用 cURL

$app_access_token = GetCH();
function GetCH(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
if(substr($url,0,8)=='https://'){
    // The following ensures SSL always works. A little detail:
    // SSL does two things at once:
    //  1. it encrypts communication
    //  2. it ensures the target party is who it claims to be.
    // In short, if the following code is allowed, CURL won't check if the 
    // certificate is known and valid, however, it still encrypts communication.
    curl_setopt($ch,CURLOPT_HTTPAUTH,CURLAUTH_ANY);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
}
$sendCH = curl_exec($ch);
curl_close($ch);
return $sendCH;
};

如果这有帮助,请告诉我。

【讨论】:

  • 非常感谢肖恩。这是一个很好的例子。在这种情况下,问题是用户错误。请参阅下一个答案。
  • 欢迎标记,请随意投票或标记为正确答案。
【解决方案2】:

我犯了一个新手错误。我没有注意到这个问题只是在我打开另一个浏览器选项卡并以我自己或测试用户身份登录到 Facebook 后发生。新的 Facebook 会话正在干扰 API。

我猜 API 使用登录用户的访问令牌,而不是请求中传递的内容。一旦你理解它就很明显了!

希望这对下一个人有帮助:-)

【讨论】:

    猜你喜欢
    • 2015-01-24
    • 2014-09-15
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2017-11-08
    • 2021-12-02
    相关资源
    最近更新 更多