Note that there are some explanatory texts on larger screens.

plurals
  1. POFacebook OAuth retrieve information based on stored session
    primarykey
    data
    text
    <p>Am new to facebook development and am trying to get this little job done. I've been using PHP-SDK so far to authenticate. </p> <p>I used the below code to gain access </p> <pre><code>&lt;?php require '../src/facebook.php'; Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' =&gt; 'xxx', 'secret' =&gt; 'xxx', 'cookie' =&gt; true, )); // We may or may not have this data based on a $_GET or $_COOKIE based session. // // If we get a session here, it means we found a correctly signed session using // the Application Secret only Facebook and the Application know. We dont know // if it is still valid until we make an API call using the session. A session // can become invalid if it has already expired (should not be getting the // session back in this case) or if the user logged out of Facebook. $session = $facebook-&gt;getSession(); $me = null; // Session based API call. if ($session) { try { $uid = $facebook-&gt;getUser(); $me = $facebook-&gt;api('/me'); } catch (FacebookApiException $e) { error_log($e); } } // login or logout url will be needed depending on current user state. if ($me) { $logoutUrl = $facebook-&gt;getLogoutUrl(); } else { //$loginUrl = $facebook-&gt;getLoginUrl(); $loginUrl = $facebook-&gt;getLoginUrl(array('ext_perm' =&gt; 'offline_access'); } // This call will always work since we are fetching public data. $naitik = $facebook-&gt;api('/naitik'); ?&gt; &lt;!doctype html&gt; &lt;html xmlns:fb="http://www.facebook.com/2008/fbml"&gt; &lt;head&gt; &lt;title&gt;php-sdk&lt;/title&gt; &lt;style&gt; body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;!-- We use the JS SDK to provide a richer user experience. For more info, look here: http://github.com/facebook/connect-js --&gt; &lt;div id="fb-root"&gt;&lt;/div&gt; &lt;script&gt; window.fbAsyncInit = function() { FB.init({ appId : '&lt;?php echo $facebook-&gt;getAppId(); ?&gt;', session : &lt;?php echo json_encode($session); ?&gt;, // don't refetch the session when PHP already has it status : true, // check login status cookie : true, // enable cookies to allow the server to access the session xfbml : true // parse XFBML }); // whenever the user logs in, we refresh the page FB.Event.subscribe('auth.login', function() { window.location.reload(); }); }; (function() { var e = document.createElement('script'); e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; e.async = true; document.getElementById('fb-root').appendChild(e); }()); &lt;/script&gt; &lt;h1&gt;&lt;a href="example.php"&gt;php-sdk&lt;/a&gt;&lt;/h1&gt; &lt;?php if ($me): ?&gt; &lt;a href="&lt;?php echo $logoutUrl; ?&gt;"&gt; &lt;img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"&gt; &lt;/a&gt; &lt;?php else: ?&gt; &lt;div&gt; Using JavaScript &amp;amp; XFBML: &lt;fb:login-button&gt;&lt;/fb:login-button&gt; &lt;/div&gt; &lt;div&gt; Without using JavaScript &amp;amp; XFBML: &lt;a href="&lt;?php echo $loginUrl; ?&gt;"&gt; &lt;img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"&gt; &lt;/a&gt; &lt;/div&gt; &lt;?php endif ?&gt; &lt;h3&gt;Session&lt;/h3&gt; &lt;?php if ($me): ?&gt; &lt;pre&gt;&lt;?php print_r($session); ?&gt;&lt;/pre&gt; &lt;h3&gt;You&lt;/h3&gt; &lt;img src="https://graph.facebook.com/&lt;?php echo $uid; ?&gt;/picture"&gt; &lt;?php echo $me['name']; ?&gt; &lt;h3&gt;Your User Object&lt;/h3&gt; &lt;pre&gt;&lt;?php print_r($me); ?&gt;&lt;/pre&gt; &lt;?php else: ?&gt; &lt;strong&gt;&lt;em&gt;You are not Connected.&lt;/em&gt;&lt;/strong&gt; &lt;?php endif ?&gt; &lt;h3&gt;Naitik&lt;/h3&gt; &lt;img src="https://graph.facebook.com/naitik/picture"&gt; &lt;?php echo $naitik['name']; ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Am storing a serialized data of the session so I can use it later on to access the wall feed. In the below code, I retrieve the session and use set_session from the API to setup the existing session. </p> <pre><code>&lt;?php require '../src/facebook.php'; require 'DBFunctions.php'; Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYPEER] = false; Facebook::$CURL_OPTS[CURLOPT_SSL_VERIFYHOST] = 2; // Create our Application instance (replace this with your appId and secret). $facebook = new Facebook(array( 'appId' =&gt; 'xxx', 'secret' =&gt; 'xxx', 'cookie' =&gt; true, )); // We may or may not have this data based on a $_GET or $_COOKIE based session. // // If we get a session here, it means we found a correctly signed session using // the Application Secret only Facebook and the Application know. We dont know // if it is still valid until we make an API call using the session. A session // can become invalid if it has already expired (should not be getting the // session back in this case) or if the user logged out of Facebook. //$session = $facebook-&gt;getSession(); $session = $facebook-&gt;setSession(unserialize(Retrieve_Session(2))); $me = null; // Session based API call. if ($session) { try { $uid = $facebook-&gt;getUser(); $me = $facebook-&gt;api('/me'); } catch (FacebookApiException $e) { error_log($e); } } // login or logout url will be needed depending on current user state. if ($me) { $logoutUrl = $facebook-&gt;getLogoutUrl(); } else { $loginUrl = $facebook-&gt;getLoginUrl(); } print_r(unserialize(Retrieve_Session(2))); ?&gt; &lt;!doctype html&gt; &lt;html xmlns:fb="http://www.facebook.com/2008/fbml"&gt; &lt;head&gt; &lt;title&gt;php-sdk&lt;/title&gt; &lt;style&gt; body { font-family: 'Lucida Grande', Verdana, Arial, sans-serif; } h1 a { text-decoration: none; color: #3b5998; } h1 a:hover { text-decoration: underline; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;&lt;a href="example.php"&gt;php-sdk&lt;/a&gt;&lt;/h1&gt; &lt;?php if ($me): ?&gt; &lt;a href="&lt;?php echo $logoutUrl; ?&gt;"&gt; &lt;img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif"&gt; &lt;/a&gt; &lt;?php else: ?&gt; &lt;div&gt; &lt;/div&gt; &lt;div&gt; Without using JavaScript &amp;amp; XFBML: &lt;a href="&lt;?php echo $loginUrl; ?&gt;"&gt; &lt;img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif"&gt; &lt;/a&gt; &lt;/div&gt; &lt;?php endif ?&gt; &lt;h3&gt;Session&lt;/h3&gt; &lt;?php if ($me): ?&gt; &lt;pre&gt;&lt;?php print_r($session); ?&gt;&lt;/pre&gt; &lt;h3&gt;You&lt;/h3&gt; &lt;img src="https://graph.facebook.com/&lt;?php echo $uid; ?&gt;/picture"&gt; &lt;?php echo $me['name']; ?&gt; &lt;h3&gt;Your User Object&lt;/h3&gt; &lt;pre&gt;&lt;?php print_r($me); ?&gt;&lt;/pre&gt; &lt;?php else: ?&gt; &lt;strong&gt;&lt;em&gt;You are not Connected.&lt;/em&gt;&lt;/strong&gt; &lt;?php endif ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The above code won't work and will show me the login screen although I've passed the unseralized data of the session. Any idea how I can solve this issue? </p> <p><em>P.S I changed the application setting to "Native Application" because I read somewhere it will handle recovering sessions without re-logging.</em></p> <p>Please advice? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload