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-
<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'});

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 -

$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);
$tmp = curl_exec($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 3935 times
Super User

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

Latest discussions

  • No posts to display.