Print this page
Saturday, 03 August 2013 22:37

how to generate facebook access token

Written by 
Rate this item
(2 votes)

Facebook access token is needed whenever you access
facebook stream of page or profile, Graph api call to post
to facebook.there are many ways by which you can generate
access token.By the following way you can create short lived
access token for a user as well as extend it to long lived
token -

by the following js sdk call you can create access token valid for
1 hour-

//before doing js sdk call initialize sdk at first written in
//this url-https://developers.facebook.com/docs/reference/javascript/
<script type="text/javascript">
FB.login(function(response) {
if (response.status=='connected') {
if (response.authResponse.accessToken) {
var token = response.authResponse.accessToken;
} else {
alert('You can create access token only for your profiles and pages.');
}
} else { alert('you have to create an access token.'); }
}, {scope:'user_groups,read_stream'});
</script>

To extend that token as long lived token(60 days) in server side code,
use the following exchange token url where $temptoken value will be the
short lived token generated above.Here i used curl call to call and get
result from the url -

$url="https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=app_id_here&client_secret=app_secret_code_here&fb_exchange_token=".$temptoken."";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$tmp = curl_exec($ch);
curl_close($ch);
echo $tmp; // $tmp contains the access token as well as other params.

Hope that helps.Don't forget to like & share the post.

Read 3910 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
7