PHP to get access token for Facebook app

Summary

To obtain a user access token for a Facebook app, developers can leverage the signed_request mechanism as an alternative to the PHP SDK's getAccessToken() method. This process involves Facebook POSTing a signed_request to the canvas page, initially with limited data, followed by a redirect for user authorization. Upon successful authorization, a subsequent signed_request containing the oauth_token is sent back to the specified redirect_url. Developers must decode this payload, check for the user_id, and handle redirection to the OAuth dialog if the user is not yet authorized. The extracted access token can then be used to make authenticated calls to the Graph API.

Since Facebook is now using OAuth 2.0 to authenticate apps to access user information. the SDK of Facebook has provided developers some useful functions to get authentication done. For example, in PHP SDK, there are getAccessToken(), getLoginUrl() etc. But unfortunately, for me I cannot use getAccessToken() method to get the user access token, it only returns me the app access token. Finally I gave up this approach to get access token for the time being. I may later retry this approach if I have time.

Today I show you another way to get the access token, which is explained on Facebook's developer website. We should use the signed_request . Since OAuth is a two way authentication mechanism, the first time, when the canvas page is loaded, a signed_request will  be POSTed to the canvas page, user can use $_REQUEST["signed_request"] to access this object. At the first time,, the signed_request will contain limited information, then the page will be redirected to the Facebook login page and authorization page. Once the authorization is done, the page will be redirected to the redirect_url and this time it will again send a signed_request object to the canvas page, and now the signed_request object will contain the access token.

An example code snippet below :

    define('APPID','XXX');
    define('APPSECRET','XXXX');
    define('CANVAS_URL','http://apps.facebook.com/appname/');

    $auth_url = "https://www.facebook.com/dialog/oauth?client_id="
    . APPID . "&redirect_uri=" . urlencode(CANVAS_URL)."&scope=user_relationships,user_relationship_details";
   
    $signed_request = $_REQUEST["signed_request"];
    list($encoded_sig, $payload) = @explode('.', $signed_request, 2);
    //DECODE THE DATA WHICH CONTAINS THE ACCESS TOKEN
    $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
    if (empty($data["user_id"])) {
        echo("<script> top.location.href='" . $auth_url . "'</script>");
    }
   
    $access_token=$data["oauth_token"];

The code above is quite straightforward, after getting the $data object, it will check whether the user_id exists or not, if it doesn't exist, it will be redirected to the $auth_url page. After authorization, the page will be redirected back to the CANVAS_URL page and now the user_id should have been set and the access token is available.

Now we can use access_token to get some information of the user using the Graph API . One example :

$user_str=file_get_contents('https://graph.facebook.com/me?access_token='.$access_token);
$user=json_decode($user_str,true);
var_dump($user);

Ok, it's done. Later I will explain how to use FQL to get gender of a friend.

PHP ACCESS TOKEN FACEOOK SIGNED REQUEST

  RELATED

  COMMENTS

4
Mad_Griffith
May 9, 2012 at 6:38 pm
Hello, I'm trying to guess how to fetch and display the picture of a group and the number of total members... do you have any clue?
Peter
May 10, 2012 at 6:12 am
I guess this one will help you. https://graph.facebook.com/$access_token/members. The returned value is an array of members in a group. The access_token here is the group's access token.
Mad_Griffith
May 11, 2012 at 4:07 am
Peter, can you help me? I didn't know how to put your advice into practice so I mixed some lines of PHP code with the help of the Facebook developers codex (but without understanding much): http://pastebin.com/5eCrWtDp for the "scope" parameter I put the values listed here: https://developers.facebook.com/docs/reference/fql/group_member/ also this is useful to build the query to the group picture: https://developers.facebook.com/docs/reference/fql/group/
kunal
Jul 17, 2012 at 6:42 am
thanks for this vital information