简体   繁体   English

Facebook JS / PHP SDK

[英]Facebook JS/PHP SDK

So I am encountering some weird problems with the Facebook SDK. 所以我在使用Facebook SDK时遇到了一些奇怪的问题。 I use both the JS SDK as the PHP SDK. 我同时使用JS SDK和PHP SDK。 To log a user out I use the following onclick: 要注销用户,我使用以下onclick:

<a href="/logout/" onclick="FB.logout ();">Log out</a>

After I clicked that link, when I try to perform any JS SDK call, like: FB.getAuthResponse (); 单击该链接后,当我尝试执行任何JS SDK调用时,例如:FB.getAuthResponse();。 it returns, as expected, null. 它按预期返回null。

But after clicking the link, when I use the PHP SDK to check if someone is logged in, or just use the following SDK function: $this -> facebook -> getAccessToken (); 但是单击链接后,当我使用PHP SDK来检查是否有人登录时,或者只是使用以下SDK函数:$ this-> facebook-> getAccessToken(); it returns a valid token as if I am still logged in. 它会返回有效令牌,就像我仍在登录一样。

What am I missing here? 我在这里想念什么? By the way, do you need some of my PHP code where I check for login? 顺便说一句,您是否需要一些我的PHP代码来检查登录?

Thanks in advance! 提前致谢!

The PHP sdk saves the access token into local session. PHP sdk将访问令牌保存到本地会话中。 That's a huge problem. 那是个大问题。 Especially in times when the facebook token expires and locally the SDK thinks it's still active. 尤其是在Facebook令牌到期且SDK在本地认为仍处于活动状态时。 The solution we use is everytime we need user data we check against the access token to see if it's still valid. 我们使用的解决方案是,每当我们需要用户数据时,我们都会对访问令牌进行检查以查看其是否仍然有效。 People would think this should be a mndatory behaviour of the SDK (to guarantee an active token) but sadly that's not the case. 人们会认为这应该是SDK的强制行为(以确保激活令牌),但不幸的是事实并非如此。 This is snippet of the code we use. 这是我们使用的代码片段。

$graph_url = "https://graph.facebook.com/me?access_token=". $access_token;

$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
//Check for errors
if ($decoded_response->error) {
  // check to see if this is an oAuth error:
  if ($decoded_response->error->type== "OAuthException") {
    // Retrieving a valid access token.
    $dialog_url= "https://www.facebook.com/dialog/oauth?"
    . "client_id=" . FB_APP_ID
    . "&redirect_uri=" . urlencode($redirect_url)
    . "&scope=" . implode(',', $permissions);
    exit("<script> top.location.href='" . $dialog_url ."'</script>");
  }
  else {
    //handle other error types
  }
else {
  return $facebook-getUser();
}

This is part of our own getUser function. 这是我们自己的getUser函数的一部分。 Hope it will help you to implement your own :) 希望它能帮助您实现自己的:)

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

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