How to Get Facebook Access Token
STEP 1 - create the redirect_uri page
First, create a simple php script in your local web server, which has the Canvas URL (redirect_uri) you set creating your Facebook App in STEP 3. I set this URL to http://localhost/fb-tokens/. My local web serverDocumentRoot is /srv/http, so:
mkdir /srv/http/fb-tokens
nano /srv/http/fb-tokens/index.php
Put the following code.
<?php
print_r($_GET);
?>
This code will just print the php $_GET array, in other words what Facebook will pass to you from query string.
STEP 2 - authorize your app (give permissions)
On your browser, give the following url, where client_id is App ID and redirect_uri is the URL defined as Canvas URL
https://www.facebook.com/dialog/oauth?client_id=167252680143474&redirect_uri=http://localhost/fb-tokens/&scope=manage_pages,publish_stream
The part &scope=manage_pages,publish_stream of this URL defines thw permissions you will assign to your app.
Read about Facebook permissions:
- an introduction here.
- about Facebook publish_stream permission here.
- about Facebook manage_pages permission here.
click OK
authorize your app to post to your friends on your behalf
click OK
authorize your app to manage your Pages
Get the code value
You will get somenthing like this
Array
(
[code] =>
AQCnhrD8RSxJRmGJdYCfFD33hGPv84xa-xqXKD1-3i3qmsPjqHODEn1cROQkYv6LSKhKlPBxkPHD9gUs-9W0GSwDJVvarX70QiLAYmcLsGhN2u0Ib1OF512TvMljg8WLjx0FaAFCB1DHiTqYE-6ZNtWqtZpe4aIecOw949QNcWbZOf2BiCH_yECCNfamKdatV5Nv1Oa1IvIi_8_zVGc_cQqujVC_O2Apkzyj7M1cypoucGc02NzpAQv3yPqCnIuz5TWglcvhr2YNL7HAClLW0ydWgmq7FyfXOfrbFLoRjd-GRTevmTCffA2iV_A1i-itStY
)
After getting the code value, give the following url, where client_id is App ID and redirect_uri is the URL defined as Canvas URL, client_secret is App Secret and code is the code
got.
https://graph.facebook.com/oauth/access_token?client_id=167252680143474&redirect_uri=http://localhost/fb-tokens/&client_secret=29d8d368420dc1e81198fce224ac71c9&code=AQCnhrD8RSxJRmGJdYCfFD33hGPv84xa-xqXKD1-3i3qmsPjqHODEn1cROQkYv6LSKhKlPBxkPHD9gUs-9W0GSwDJVvarX70QiLAYmcLsGhN2u0Ib1OF512TvMljg8WLjx0FaAFCB1DHiTqYE-6ZNtWqtZpe4aIecOw949QNcWbZOf2BiCH_yECCNfamKdatV5Nv1Oa1IvIi_8_zVGc_cQqujVC_O2Apkzyj7M1cypoucGc02NzpAQv3yPqCnIuz5TWglcvhr2YNL7HAClLW0ydWgmq7FyfXOfrbFLoRjd-GRTevmTCffA2iV_A1i-itStY
You will get the Access Token (!)
access_token=CAACYHYyWcnIBAJ1LwRLTERQNXJ4qRCqoTf2pgs1V2AiZCOreWAH0bY2UKaoyD3elfcEZAZAs6fZAEYPAZC5OlU6ZCg8Org32D13LiencxZA0PsNzkQPPuZCiZAukgMnqLXM0F2ZBlYZAnZB08qVwOo6DgiCJSlkoZCb9VVs0ARsleZBcduzTQKZBvIcvjLZBXXr5ye8R4FcZD&expires=5183831