Facebook PHP SDK处理访问令牌 [英] Facebook PHP SDK dealing with Access Tokens
问题描述
我已经爬过了很多各种各样的答案,但仍然有点困惑,我应该如何处理Facebook 访问令牌。
我遇到的主要问题之一是由于我的浏览器中存储了什么信息。例如,我登录到应用程序,令牌过期,我无法再登录,除非我清除浏览器中的cookies /应用程序设置。
I have crawled around lots of various answers but am still a bit confused with how I should be dealing with facebook access tokens. One of the main problems I'm having is due to what information is being stored in my browser. For example, I log onto the app, the token expires, I can't logon again unless I clear cookies/app settings in browser.
我偶然发现这个主题:如何从offline_access弃用后扩展访问令牌的有效性
I stumbled across this thread: How to extend access token validity since offline_access deprecation
哪个显示了如何通过php创建扩展访问令牌
Which has shown me how to create an extended access token through php.
我的问题是:
1。 >我需要在任何地方存储访问令牌吗?
1. Do I need to store the access token anywhere?
2。访问令牌到期或变得无效时会发生什么?目前,我的应用程序在短期访问期满时就会停止工作。
2. What happens when the access token expires or becomes invalid? At the moment, my app simply stops working when the short term access ones expire.
3。有没有办法处理它们来检查他们是否已经过期?
我正在使用php sdk,并基本使用标准if($ user)...像这样:
3. Is there a way I should be handling them to check if they have expired? I am using the php sdk and have basically used the standard if( $user )... Like this:
require 'sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
if( $user ){
try{
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user){
$params = array(
'scope' => 'email',
);
$loginUrl = $facebook->getLoginUrl( $params );
echo '<script type="text/javascript">
window.open("'. $loginUrl .'", "_self");
</script>';
exit;
}
if( $user ){
$access_token = $facebook->getExtendedAccessToken();
$get_user_json = "https://graph.facebook.com/me?access_token="
. $access_token;
// Rest of my code here...
}
- 还有什么我应该在处理令牌吗?
。我应该在页面之间传递访问令牌,还是可以在每个页面的顶部再次调用它,如下所示:
. Should I be passing the access token between pages or is it ok to just call it again at the top of each page like this:
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXX',
'redirect_uri' => 'http://localhost:8000/',
));
$token = $facebook->getExtendedAccessToken();
推荐答案
让我们解决你的问题:
我需要在任何地方存储访问令牌吗?
Do I need to store the access token anywhere?
这取决于你的应用程序。首先问自己,当他不是存在(未登录到您的应用程序)时,您是否需要代表用户执行操作?
如果答案是是,那么您需要扩展使用PHP-SDK的用户令牌,方法是在您有一个有效的用户会话时调用此方法: setExtendedAccessToken()
。
This depends on your application. First of all ask yourself, do you need to perform actions on behalf of the user while he is not present (not logged in to your app)?
If the answer is yes, then you need to extend the user token which can be done using the PHP-SDK by calling this method while you have a valid user session: setExtendedAccessToken()
.
您还应该参考本文档:扩展访问令牌
Also you should refer to this document: Extending Access Tokens
访问令牌到期或变得无效时会发生什么? ...
有没有办法处理他们来检查他们
是否已经过期?
What happens when the access token expires or becomes invalid? ... Is there a way I should be handling them to check if they have expired?
这就是您的代码中的 catch 子句派上用场,而Facebook的例子只有日志错误( error_log($ e);
)你应该处理它!
This is where the catch clause in your code comes in handy, while facebook example only logs the error (error_log($e);
) you should be handling it!
Facebook已经有一个关于这个的教程:操作方法:处理过期的访问令牌。
Facebook already has a tutorial about this: How-To: Handle expired access tokens.
此外,您还应该参考错误表< a>并相应调整您的代码。
Also you should refer to the Errors table and adjust your code accordingly.
我还应该做些什么来处理令牌?
Is there anything else I should be doing to handle tokens?
见上文。
我应该在页面之间传递访问令牌,还是只要
再次调用每个页面的顶部
Should I be passing the access token between pages or is it ok to just call it again at the top of each page
您不需要执行任何操作,因为PHP-SDK将为您处理令牌;你注意到你正在调用: $ user_profile = $ facebook-> api('/ me');
而不附加用户 access_token
?
You shouldn't need to do any of that, because the PHP-SDK will handle the token for you; have you noticed that you are calling: $user_profile = $facebook->api('/me');
without appending the user access_token
?
SDK从最后添加它,所以您不用担心。
The SDK is adding it from its end so you don't have to worry about it.
这篇关于Facebook PHP SDK处理访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!