461062-3-ways-to-fight-facebook-fatigue

Facebook Graph API – how to generate app token

Part 2 in a series of articles: Social Media APIs [developers]. And today: How to simply generate Facebook API token.

So we learned how to crawl into another website with PHP CURL function, and we also understood that Facebook PHP SDK is not really the best way to hook to Facebook Graph API. Today we will show you how to simply generate a Facebook API token.

  1. What is a Facebook API token?

When you want your app to make API calls, you must generate a Facebook App Token. The token is basically your way to access the API without exposing your app-id and app-password to the public.

Once you generated the token, you can send it as part of your API calls (we will show examples in future articles) and get the appropriate response from Facebook. Note that a Facebook access token has limited lifetime –  and therefore once generated, you need to make an API calls that extends it.

  1. How to generate a token with direct API call?

The easiest way to generate an APP access token, is to do a direct https request. Open your internet browser and type in the following address:

https://graph.facebook.com/oauth/access_token?client_id=AAAAAAA&client_secret=BBBBBBB&grant_type=client_credentials

While you replace AAAAAAA with your app ID, as taken from your Facebook Developers backoffice, and BBBBBBBB is your app secret:

facebook_api_app_details_and_secret

The response you would get will be something like looks like that:

facebook_api_app_details_response

Which is your access token. Note: never publish your app ID and app secret. We used here faked screen shots.

  1. Generic function for Facebook API token generation

The following function will do the job for an ongoing development work that you may require:

function facebook_generate_app_token($app_id=”,$app_secret=”)
{
global $G_facebook_app_id,$G_facebook_app_secret;
if (isset($app_id) AND $app_id=”){
$app_id = $G_facebook_app_id;
}else{
$app_id = $G_facebook_app_id;
}
if (isset($app_secret) AND $app_secret=”){
$app_secret = $G_facebook_app_secret;
}else{
$app_secret = $G_facebook_app_secret;
}
$url = “https://graph.facebook.com/oauth/access_token?client_id=”.$app_id.”&client_secret=”.$app_secret.”&grant_type=client_credentials”;
$response = curl_to_api($url);
$access_token = str_replace(“access_token=”,””,$response);
return $access_token;
}

To call the function,  use:

$access_token =  facebook_generate_app_token($app_id,$app_secret);

Stay tuned for the next article: how to extend a Facebook App Token

Comments

comments

Leave a Reply

Your email address will not be published. Required fields are marked *