{"id":1742,"date":"2022-08-30T15:19:04","date_gmt":"2022-08-30T15:19:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/30\/using-fb-sdk-php-javascript-together-process-oauthexception-active-access-token-must-be-used-to-query-information-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:04","modified_gmt":"2022-08-30T15:19:04","slug":"using-fb-sdk-php-javascript-together-process-oauthexception-active-access-token-must-be-used-to-query-information-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/using-fb-sdk-php-javascript-together-process-oauthexception-active-access-token-must-be-used-to-query-information-collection-of-common-programming-errors\/","title":{"rendered":"Using FB SDK (PHP &amp; Javascript Together) process OAuthException &ldquo;active access token must be used to query information&rdquo;-Collection of common programming errors"},"content":{"rendered":"<p>Like many people I&#8217;ve found across stack overflow I&#8217;ve been experiencing an issue where after about 10-15 mins of time the users get randomly logged out and brought back to the &#8220;Login with Facebook&#8221; page where the button does not work without a press and a manual refresh.<\/p>\n<p>I setup the <code>try { }<\/code> of the <code>getUser()<\/code> to log any eception and what I found was the general error:<\/p>\n<pre><code>OAuthException: An active access token must be used to query information about the current user.\n<\/code><\/pre>\n<p>I quickly searched the net trying find a solution for this error of miss matched information. I <strong>did try<\/strong> the solutions of checking for <code>$facebook-&gt;getUser()<\/code> instead of <code>facebook-&gt;api('\/me')<\/code> as well as alway adding the Access Token to requests but clearly there is still something wrong.<\/p>\n<p>I did change the <code>$facebook-&gt;api('\/me')<\/code> call to <code>$facebook-&gt;api('\/'.$FacebookUserID)<\/code> and that worked for a while about an hour and then the user would see a different error:<\/p>\n<pre><code>PHP Fatal error:  Uncaught Exception: 102: Requires user session thrown in \/base_facebook.php on line 1249\n<\/code><\/pre>\n<p>Which is pretty much the same error but instead of bringing me back to the login screen it brought me to a PHP error page.<\/p>\n<p>Does anyone have any ideas or advice on how to fix this issue?<br \/>\nThank You!<\/p>\n<p><strong>Here is the code I am using (broken down in a MVC setup)<\/strong><br \/>\n(1) The PHP Controller loads the PHP SDK upon loading of the website and sets <code>$fBSdk = $fb-&gt;fBSdk()<\/code> which calls the method below.<\/p>\n<pre><code>public function fBSdk() {\n  $this-&gt;facebook = new Facebook(array(\n    'appId'  =&gt; '1234567890',\n    'secret' =&gt; 'asdf123qwerty', \n    'cookie' =&gt; true,\n  ));\n  $this-&gt;user = $this-&gt;facebook-&gt;getUser(); \/\/Store \"$user\" in FB Class\n  if ($this-&gt;user) {\n    try {\n      $this-&gt;user_profile = $this-&gt;facebook-&gt;api('\/me');\n    } catch (FacebookApiException $e) {\n\n      \/\/$this-&gt;user = null; user) {\n    return true;\n  }\n  else {\n    return false;\n  }\n}\n<\/code><\/pre>\n<p>(2) The <code>$fBSdk<\/code> variable is then checked to see if it is <code>=== false<\/code>, which is is and the login button is loaded as well as the Javascript SDK<\/p>\n<pre><code>if ($fBSdk === false) {\n    \/\/[... Code for the LOG IN Page &amp; Button ...]\n    echo 'Login with Facebook';\n}\n<\/code><\/pre>\n<p>(3) The Login page loads including the Javascript SDK:<\/p>\n<pre><code>\n\n  window.fbAsyncInit = function() {\n    FB.init({\n      appId: '1234567890',\n      channelUrl: '\/\/www.someDomain.com\/dev\/channel.php', \/\/Yes this is the correct Link\n      status: true,\n      cookie: true,\n      xfbml: true,\n      oauth: true,\n    });\n\n    \/\/Used to Reload AFTER login (doesn't fire with above issue)\n    FB.Event.subscribe('auth.login', function(response) {\n      window.location.reload();\n    });\n    FB.Event.subscribe('auth.logout', function(response) {\n      window.location.reload();\n    });\n\n    FB.Canvas.setSize({ width: 810 });\n    FB.Canvas.setAutoGrow();\n  };\n\n  (function(d, debug){\n     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]\n     if (d.getElementById(id)) {return;}\n     js = d.createElement('script'); js.id = id; js.async = true;\n     js.src = \"\/\/connect.facebook.net\/en_US\/all\" + (debug ? \"\/debug\" : \"\") + \".js\";\n     ref.parentNode.insertBefore(js, ref);\n   }(document, \/*debug*\/ false));\n\n<\/code><\/pre>\n<p>(5) The user information is used with in the app in various places the most common and model example, as they are all used pretty close is <code>getID()<\/code> and <code>getFirstName()<\/code> who has code that look like:<\/p>\n<pre><code>\/\/Note: $this-&gt;user_profile is in the FB class and\n\/\/      is set in set on Line 7 of Example 1 above.\npublic function getID() {\n  return $this-&gt;user_profile['id'];\n}\n<\/code><\/pre>\n<p>(6) The user then clicks the login button and controller is fired again, the <code>$fBSdk<\/code> variable is checked again and the variable is set to <code>true<\/code> thus firing the <code>else { ... }<\/code> statement.<\/p>\n<p>Thanks!<\/p>\n<p><strong>UPDATE 1:<\/strong><br \/>\nBack to square 1, it seems nothing is really working so back to the place I got the code, the php sdk: https:\/\/github.com\/facebook\/facebook-php-sdk\/blob\/master\/examples\/with_js_sdk.php<\/p>\n<p><strong>UPDATE 2:<\/strong><br \/>\nSo, I get the same error in the same of code that I get in my app, about 10 mins and it times out. weird. I found the issue in facebook bugs but cannot find a fix. https:\/\/developers.facebook.com\/bugs\/404450666302585<\/p>\n<p>Thoughts?<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-30 17:54:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Like many people I&#8217;ve found across stack overflow I&#8217;ve been experiencing an issue where after about 10-15 mins of time the users get randomly logged out and brought back to the &#8220;Login with Facebook&#8221; page where the button does not work without a press and a manual refresh. I setup the try { } of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1742","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1742","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1742"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1742\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1742"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1742"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1742"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}