简体   繁体   English

Facebook给了我两个不同的acces令牌,PHP SDK

[英]Facebook gives me two different acces tokens, PHP SDK

I am using Yii as framework but i think that wouldn't mather. 我正在使用Yii作为框架,但是我认为那不会。

At this point i use the PHP SDK to get the accounts information from an user, this works fine while my user logged in at first. 此时,我使用PHP SDK从用户处获取帐户信息,这在我的用户首次登录时可以正常工作。 But after 1 or 2 hours the access token expires and i get an error (which isn't weird). 但是在1或2个小时后,访问令牌到期,并且我得到一个错误(这并不奇怪)。

But here is where i get stuck and i can think of 2 solutions but both i have no clue how to do them. 但是在这里我被困住了,我可以想到2个解决方案,但是我都不知道如何做。 First let me introduce some of my code: 首先让我介绍一些代码:

To log in to my app users are send to the FacebookLogin() action: 要登录我的应用,用户需要发送至FacebookLogin()操作:

public function actionFacebookLogin()
{
    $my_url = 'http://***.***.nl/app/facebook-popup.php';
    $permissions = 'read_insights, publish_actions, manage_pages, email';
    $loginUrl = Yii::app()->facebook->getLoginUrl(array('scope' => $permissions, 'display' => 'popup', 'redirect_uri' => $my_url));
    echo '<script> window.location="'.$loginUrl.'"</script>';
}

After the user granted the proper permissions, the user is send to the Login() action: 在用户授予适当的权限之后,该用户将被发送到Login()操作:

public function actionLogin() {
    $facebookID = Yii::app()->facebook->getUser();  
    $accessToken = Yii::app()->facebook->getAccessToken();
}

(The rest of the login action isn't important.) (其余的登录操作并不重要。)

Now at this point the ->getAccessToken() function gives me an proper access_token with a short life time. 现在,这时->getAccessToken()函数为我提供了一个使用寿命短的正确access_token。 This would be fine unless the user is longer then 1 or 2 hours on my page, "some weird stuff now* after that the user has to logout and login back again. Then i still get the error except after around 10 minutes i get an new access_token which do grant me the needed permissions. 除非用户在我的页面上停留的时间超过1或2个小时,否则就可以了,“现在有些怪异的东西*,之后用户必须注销并再次登录。然后我仍然会收到错误消息,除非大约10分钟后我得到了新的access_token确实会授予我所需的权限。

So to solve this i thought of 2 solutions, which i am unable to find the proper approach to use this. 因此,为了解决这个问题,我想到了2个解决方案,但我找不到适合的解决方案。

First the easiest one. 首先是最简单的。 When i go to the Graph API Explorer and use the access_token my app gives me i get the info, but when i try to debug that access_token it shows that the access_token has an experation date. 当我转到Graph API Explorer并使用access_token时,我的应用程序会为我提供信息,但是当我尝试调试该access_token时,它表明access_token具有有效日期。 But when i use the same Graph API Explorer and let it generate an access_token on its own i get an access_token that doesn't expire at all. 但是,当我使用相同的Graph API Explorer并让它自己生成一个access_token时,我得到的access_token根本不会过期。

As asked an update on my handlings with the Graph API Explorer I go to the API Explorer and change the app to my webapp. 当询问有关使用Graph API Explorer进行处理的更新时,我转到API Explorer并将该应用程序更改为我的webapp。 Then it got an normal access_token filled in automaticly. 然后,它会自动填充一个普通的access_token。 When i press the "debug" button it says that this access_token has an experation date. 当我按下“调试”按钮时,它说此access_token有一个截止日期。 When i then go back to the Explorer and press the "get access token" button it gives me an other one. 然后,当我返回浏览器并按“获取访问令牌”按钮时,它又给了我另一个。 When i then again press the "debug" button it says that this access_token doesn't have an experation date. 当我再次按下“调试”按钮时,它表示此access_token没有到期日期。 Which would be great if i can use this one. 如果我可以使用这一个,那会很棒。

So my first solution would be to get that last access_token (if possible) and store it in my database. 因此,我的第一个解决方案是获取最后一个access_token(如果可能)并将其存储在我的数据库中。 Then when needed use the Facebook PHP SDK ->setAccessToken() function. 然后在需要时使用Facebook PHP ->setAccessToken()函数。 But how?? 但是如何? I don't have a clue! 我没有头绪!

Second solution i thought of. 我想到的第二个解决方案。 I could also make an init() function which checks on every action if we got an valid access_token and if not get a new one or refresh the current one which is expired. 我还可以创建一个init()函数来检查每个动作,如果我们得到一个有效的access_token,如果没有得到一个新的,或者刷新一个过期的当前动作。 This would have more server load then the first solution, and same with the first solution i have no clue how to achieve this. 与第一个解决方案相比,这将具有更多的服务器负载,并且与第一个解决方案相同,我不知道如何实现此目的。

I think that the main problem is that i find the Facebook docs kinda hard to understand, and i have no clue where to start from. 我认为主要的问题是我发现Facebook文档有点难以理解,而且我也不知道从哪里开始。

Any input would be very much appriciated! 任何输入将非常有用!

You can either prolong the expiration time of the access token, as described here: https://developers.facebook.com/roadmap/offline-access-removal/ 您可以按照以下说明延长访问令牌的到期时间: https : //developers.facebook.com/roadmap/offline-access-removal/

Or you can embed the JS SDK, which on every page call will get a fresh short lived access token if the previous one has expired. 或者,您可以嵌入JS SDK,如果前一个过期,则在每个页面调用中将获得一个新的短期访问令牌。 (And shares it with the PHP SDK via cookie.) (并通过cookie与PHP SDK共享。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM