Facebook PHP SDK:获得“长寿命”现在访问令牌“offline_access”已被弃用 [英] Facebook PHP SDK: getting "long-lived" access token now that "offline_access" is deprecated

查看:122
本文介绍了Facebook PHP SDK:获得“长寿命”现在访问令牌“offline_access”已被弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本问题:我希望我的应用程序即使在用户离开的时候也可以打电话给有关授权用户的Facebook图形API。



例如,我想让用户(A)授权该应用,然后我希望用户(B)能够使用该应用查看有关用户的信息(A)的朋友具体来说:工作领域。是的,我要求这些扩展权限( user_work_history friends_work_history 等)。目前,我的应用程序可以访问登录的用户的朋友的工作历史,但不能访问应用程序的其他用户的任何朋友的工作历史。



以下是我已经知道的:





这是我不知道的(我希望您可以告诉我):
如何使用Facebook PHP SDK获取扩展(又名长寿命)访问令牌?目前,我的代码如下所示:

  $ facebook-> getAccessToken(); 

有这样的事情吗?:

  $ facebook-> getExtendedAccessToken(); 

如果不是,这是我应该做什么?

  $ accessToken = $ facebook-> getAccessToken(); 
$ extendedAccessToken = file_get_contents(https://graph.facebook.com/oauth/access_token?
client_id = {$ appId}&
client_secret = {$ secret}&
grant_type = fb_exchange_token&
fb_exchange_token = {$ accessToken}
);

我已经尝试了,它不起作用。我收到这个错误:

 警告:file_get_contents(https://graph.facebook.com/oauth/access_token?client_id =# ######& client_secret = #########& grant_type = fb_exchange_token& fb_exchange_token = ##########)[function.file-get-contents]:未能开放流:HTTP请求失败! HTTP / 1.0 400 Bad Request in / ... 

如果我切换到FQL,它的工作方式会有所不同而不是图api?我已经阅读了Facebook文档多次,但PHP sdk并没有被彻底的记录,我找不到任何例子,这将如何工作。

解决方案

我终于想到了自己。答案是相当反高潮。看起来,新创建的应用程序会自动获得60天访问令牌。我不知道这是否依赖于在应用设置的迁移部分启用depricate offline_access设置。让它安全。



所以在写这篇文章时,你可以使用PHP SDK,如下所示: $ facebook-> getAccessToken();



(我的应用程序无法正常工作的原因与访问令牌的到期无关) p>

还有一件事,要使用PHP SDK获取长期访问令牌,您应该调用 $ facebook-> setExtendedAccessToken(); before $ facebook-> getAccessToken();


BASIC PROBLEM: I want my app to be able to make calls to the Facebook graph api about authorized users even while the user is away.

For example, I want the user (A) to authorize the app, then later I want user (B) to be able to use the app to view info about user (A)'s friends. Specifically: the "work" field. Yes, I am requesting those extended permissions (user_work_history, friends_work_history, etc). Currently my app has access to the logged-in user's friends work history, but not to any of the friends' work history of other users of the app.

Here's what I know already:

  • Adding offline_access to the scope parameter is the old way and it no longer works.
  • The new way is with "long-lived" access tokens, described here. These last for 60 days.
  • I need to exchange a normal access token to get the new extended token. The FB documentation says:

    https://graph.facebook.com/oauth/access_token?
    client_id=APP_ID& client_secret=APP_SECRET& grant_type=fb_exchange_token& fb_exchange_token=EXISTING_ACCESS_TOKEN

Here's what I don't know (and I'm hoping you can tell me): How do I get the extended (aka "long-lived") access token using the Facebook PHP SDK? Currently, my code looks like this:

$facebook->getAccessToken();

Is there such a thing as this?:

$facebook->getExtendedAccessToken();

If not, is this what I should be doing?

$accessToken = $facebook->getAccessToken();
$extendedAccessToken = file_get_contents("https://graph.facebook.com/oauth/access_token?             
    client_id={$appId}&
    client_secret={$secret}&
    grant_type=fb_exchange_token&
    fb_exchange_token={$accessToken}"
    );

I've tried it and it doesn't work. I get this error:

Warning: file_get_contents(https://graph.facebook.com/oauth/access_token? client_id=#######& client_secret=#########& grant_type=fb_exchange_token& fb_exchange_token=##########) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /...

Does it work any differently if I switch to FQL instead of the graph api? I've read through the Facebook documentation many times, but the PHP sdk is not thoroughly documented and I can't find any examples of how this should work.

解决方案

I finally figured this out on my own. The answer is pretty anti-climactic. It appears that newly created apps get 60 day access tokens automatically. I'm not sure if this is dependent on enabling the "depricate offline_access" setting in the Migrations section of the app settings. Leave it on to be safe.

So at the time of writing this, you can use the PHP SDK as follows: $facebook->getAccessToken();

(The reason my app wasn't working as expected was unrelated to the expiration of the access token.)

Just one more thing, to get long-lived access token using PHP SDK you should call $facebook->setExtendedAccessToken(); before $facebook->getAccessToken();

这篇关于Facebook PHP SDK:获得“长寿命”现在访问令牌“offline_access”已被弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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