Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP SDK: How do I capture the access token after user auths app?
    primarykey
    data
    text
    <p>This is for a canvas app on the Facebook Platform using the new(est) Facebook PHP SDK.</p> <p>We are using the PHP example from the Facebook tutorial (https://developers.facebook.com/docs/appsonfacebook/tutorial/) to trigger the OAuth dialog and get the test user to the redirect URL.</p> <p>At the redirect URL, we use the PHP example from the Facebook signed request docs page (https://developers.facebook.com/docs/authentication/signed_request/) and our test users can successfully authorize the app.</p> <p>However, after the test user auths the app, we are not able to capture the access token and its expiration. We can see it in the address bar appended to the redirect URL, but it does not show up in the $_REQUEST array. If we add {$access_token = $facebook->getAccessToken();} to the redirect URL page, it shows a value for the access token, but the value it shows is not the full token string that we see when we click on Show Token in the Test User Roles page (which we believe is the correct access token for the test user).</p> <p>Here is an example of the redirect URL with an access token appended: <a href="http://karmakorn.com/karmakorn/alpha20/kk-fb-auth.php#access_token=126736467765%7C2.AQDavId8oL80P5t9.3600.1315522800.1-100002908746828%7CJICJwM1P_97tKmqkEO5pXDCf-7Y&amp;expires_in=6008" rel="nofollow">http://karmakorn.com/karmakorn/alpha20/kk-fb-auth.php#access_token=126736467765%7C2.AQDavId8oL80P5t9.3600.1315522800.1-100002908746828%7CJICJwM1P_97tKmqkEO5pXDCf-7Y&amp;expires_in=6008</a></p> <p>Here is what var_dump shows for the $<em>REQUEST array for that same page: array(3) { ["</em>_qca"]=> string(26) "P0-709927483-1291994912966" ["__switchTo5x"]=> string(2) "30" ["PHPSESSID"]=> string(26) "euois02ead39ijumca7nffblh2" }</p> <p>We have no idea why the $_REQUEST array varies from the values appended to the URL, and more importantly -- how to capture the access token and its expiration date.</p> <p>Can someone show us a working example of how they capture this data after running the parse_signed_request($signed_request, $secret) function on the redirect page? Thanks!</p> <p>ADDITIONAL INFO:</p> <p>Here is the pertinent code from A) our test index page, and B) our test redirect page. If we use our text index page as the redirect url it gets stuck in an endless loop -- because the user is never identified.</p> <p>A) Index Page</p> <pre><code>// Create kk-fb app instance $facebook = new Facebook(array( 'appId' =&gt; KKFB_ID, 'secret' =&gt; KKFB_KY, 'oauth' =&gt; true, )); $app_id = KKFB_ID; $secret = KKFB_KY; $canvas_auth = 'http://karmakorn.com/karmakorn/alpha20/kk-fb-auth.php'; $auth_url = "https://www.facebook.com/dialog/oauth?" . "client_id=" . $app_id . "&amp;redirect_uri=" . urlencode($canvas_auth) . "&amp;response_type=token" . "&amp;scope=email,publish_stream"; $signed_request = $_REQUEST["signed_request"]; list($encoded_sig, $payload) = explode('.', $signed_request, 2); $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); if (empty($data["user_id"])) { echo("&lt;script&gt; top.location.href='" . $auth_url . "'&lt;/script&gt;"); } else { echo ("Welcome User: " . $data["user_id"]); } </code></pre> <p>B) Redirect Page</p> <pre><code>// Create kk-fb app instance $facebook = new Facebook(array( 'appId' =&gt; KKFB_ID, 'secret' =&gt; KKFB_KY, 'oauth' =&gt; true, )); $app_id = KKFB_ID; $secret = KKFB_KY; $signed_request = $_REQUEST["signed_request"]; list($encoded_sig, $payload) = explode('.', $signed_request, 2); $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); $user = $facebook-&gt;getUser(); $access_token = $facebook-&gt;getAccessToken(); echo "User: $user &lt;br&gt;"; echo "Access Token: $access_token &lt;br&gt;"; echo "Signed Request: $signed_request &lt;br&gt;"; var_dump($_REQUEST); </code></pre> <p>Here is what shows up as these echo results:</p> <p>User: 0 Access Token: 126736467765|<strong>**<em>SECRET</em>**</strong> Signed Request: array(3) { ["_<em>qca"]=> string(26) "P0-709927483-1291994912966" ["</em>_switchTo5x"]=> string(2) "30" ["PHPSESSID"]=> string(26) "frugi545cdl15gjind1fnv6pq1" }</p> <p>Interestingly, when the test user goes back to the index page the if condition is satisfied and we can get the correct access token:</p> <p>Welcome User: 100002908746828 Access Token: 126736467765|2.AQBgcyzfu75IMCjw.3600.1315544400.1-100002908746828|m5IYEm976tJAkbTLdxHAhhgKmz8 </p> <p>Obviously, we are still missing something!? Also, we need to learn how to get the expiration time as a variable too so we can store both of these in our database.</p>
    singulars
    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.
 

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