简体   繁体   English

Facebook的access_tokens在PHP和JS的diffeerent?

[英]facebook access_tokens in php and js diffeerent?

I am going crazy here, 我要疯了

i dont understand how facebook handles the token thing. 我不明白Facebook如何处理令牌的事情。 I use php sdk for web login. 我使用php sdk进行网络登录。 This works, i can get with 这可行,我可以接受

$facebook->api('/me');

the data from user etc. 来自用户等的数据

But after a short time i do get the * fb error: 但是不久之后,我确实得到了* fb错误:

OAuthException: An active access token must be used to query information about the current user.

even though the user is still logged in. When i disconnect and login with facebook again, i do not get a new token (i think because the cookie is still active?) 即使用户仍在登录。当我断开连接并再次登录Facebook时,我也没有获得新令牌(我认为是因为cookie仍处于活动状态?)

When i delete all cookies it works for a while. 当我删除所有cookie时,它会工作一段时间。

BUT when i check back with JS SDK 但是当我使用JS SDK进行检查时

FB.getLoginStatus(function(response){FB.api('/me', function(response) {});});

I always have an active access_token and within it i can call the api, but only within... 我总是有一个活动的access_token,在其中我可以调用api,但只能在其中...

Why i cannot use the same token in php and js or why are they different????? 为什么我不能在php和js中使用相同的令牌,或者为什么它们不相同?

Please help me. 请帮我。

EDIT 编辑

To explain it better. 为了更好地解释它。 In php the access_token changes from eg 'CDAtEg' etc. to '148234' etc. (dont know why). 在php中,access_token从“ CDAtEg”等更改为“ 148234”等(不知道为什么)。 if i do the api request in php i got the OAuthException but in js SDK the old Token 'CDAtEg' is still active and i got the user_data till the token exprires. 如果我在php中执行api请求,则会收到OAuthException异常,但在js SDK中,旧令牌'CDAtEg'仍处于活动状态,并且我会获得user_data直到令牌过期。

Why i cannot use the same token in php again and why it is deleted? 为什么我不能在php中再次使用相同的令牌,为什么将其删除? I do not find the reason for that. 我没有找到原因。

EDIT 2 编辑2

Thank you for your ideas so far. 到目前为止,谢谢您的想法。 I found out two things now. 我现在发现了两件事。 First, when i save the token i get from JS SDK and use it with 首先,当我保存令牌时,我从JS SDK中获取并用于

$facebook->setAccessToken()

it seems to work so far. 到目前为止,它似乎仍然有效。 Second, as phwd posted below the user_access token i got from php is deleted after 2,3 minutes. 其次,如phwd所述,我从php获得的user_access令牌下方发布了2,3分钟后被删除。 How can i found the origin of this problem? 我如何找到此问题的根源?

Both tokens expire because they are the same user token. 这两个令牌都将到期,因为它们是相同的用户令牌。

In the PHP SDK, if the token expires or gets thrown out for some error, the PHP SDK will set the default token to the application token 在PHP SDK中,如果令牌过期或因某些错误而抛出,PHP SDK会将默认令牌设置为应用程序令牌

https://github.com/facebook/facebook-php-sdk/blob/master/src/base_facebook.php#L389 https://github.com/facebook/facebook-php-sdk/blob/master/src/base_facebook.php#L389

  /**
   * Determines the access token that should be used for API calls.
   * The first time this is called, $this->accessToken is set equal
   * to either a valid user access token, or it's set to the application
   * access token if a valid user access token wasn't available.  Subsequent
   * calls return whatever the first call returned.
   *
   * @return string The access token
   */
  public function getAccessToken() {
    if ($this->accessToken !== null) {
      // we've done this already and cached it.  Just return.
      return $this->accessToken;
    }

    // first establish access token to be the application
    // access token, in case we navigate to the /oauth/access_token
    // endpoint, where SOME access token is required.
    $this->setAccessToken($this->getApplicationAccessToken());
    $user_access_token = $this->getUserAccessToken();
    if ($user_access_token) {
      $this->setAccessToken($user_access_token);
    }

    return $this->accessToken;
  }

Thus you must introduce login handling in PHP to ensure no calls are taken unless a valid user access token is supplied. 因此,您必须在PHP中引入登录处理,以确保除非提供了有效的用户访问令牌,否则不会进行任何调用。

In JS SDK, most setups unless you explicitly set it otherwise will push the user through login after token expiry. 在JS SDK中,除非您明确设置,否则大多数设置都会在令牌到期后将用户推送通过登录。

As you describe it seems as though an error is pushing the PHP SDK to use the default application token. 正如您所描述的,似乎是错误促使PHP SDK使用默认应用程序令牌。

You can even sure that the user access token being used in the JS SDK will work in the PHP SDK if it is indeed still active , by setting it 您甚至可以通过设置它来确保JS SDK中使用的用户访问令牌在确实仍然处于活动状态的情况下也可以在PHP SDK中使用。

$facebook->setAccessToken('YOUR_ACCESS_TOKEN_IN_JS_SDK');

Once this works then the error elsewhere in your code (which you have not supplied). 一旦成功,错误就会出现在代码的其他地方(您尚未提供)。

After some time-interval access token gets expired. 经过一定时间间隔后,访问令牌将过期。 Facebook gives new access token for every session. Facebook为每个会话提供新的访问令牌。 Access token is not always same for each user and each session. 每个用户和每个会话的访问令牌并不总是相同的。

BUT when i check back with facebook SDK 但是当我使用Facebook SDK进行检查时

This is happens because FB.api check is your access_token valid and if not valid update it. 发生这种情况是因为FB.api检查是您的access_token有效,如果无效,请对其进行更新。

In php you need to do this manually. 在php中,您需要手动执行此操作。 More with code examples PHP SDK: How do I capture the access token after user auths app? 有关代码示例的更多信息PHP SDK:在用户验证应用程序后如何捕获访问令牌?

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

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