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 server<?php print_r( $_GET ); ?> |
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 URLRead about Facebook permissions:
- an introduction here.
- about Facebook publish_stream permission here.
- about Facebook manage_pages permission here.
authorize your app to access your profile
(click
the
thumb for full image)
click OKauthorize your app to post to your friends on your behalf
(click
the
thumb for full image)
click OKauthorize your app to manage your Pages
(click
the
thumb for full image)
Get the code value
(click
the
thumb for full image)
You will get somenthing like this
1
2
3
4
5
| 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.
(click
the
thumb for full image)
You will get the Access Token (!)
1
| access_token=CAACYHYyWcnIBAJ1LwRLTERQNXJ4qRCqoTf2pgs1V2AiZCOreWAH0bY2UKaoyD3elfcEZAZAs6fZAEYPAZC5OlU6ZCg8Org32D13LiencxZA0PsNzkQPPuZCiZAukgMnqLXM0F2ZBlYZAnZB08qVwOo6DgiCJSlkoZCb9VVs0ARsleZBcduzTQKZBvIcvjLZBXXr5ye8R4FcZD&expires=5183831 |
STEP 3 - Access Tokens for your Facebook Pages (if any)
If you have any Facebook Pages (except your Personal Profile), you can get Access Tokens for them, using the following URL, where access_token is the Access Token you just got for your Personal ProfileThe result will be the following JSON string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| { "data" : [ { "category" : "Website" , "name" : "pontikis.net" , "access_token" : "CAACYHYyWcnIBAMK1y2tqiRKx8bBXGFFzjdUamOlMZCBJrTSL8ic1z5sZBarBi3DbTh9mMUz3aiZCAQRNHvOmcMxLZC53FNtkrVCq8rZCLsyjbQVZAt8o7S6Rd1UT0LK7AkgyZAlu11MC9rWND8eZBiKjjiYjwmBMLWko7k6GGPZCREehKRNFCsyM1Ll7sFb1hXycZD" , "perms" : [ "ADMINISTER" , "EDIT_PROFILE" , "CREATE_CONTENT" , "MODERATE_CONTENT" , "CREATE_ADS" , "BASIC_ADMIN" ], "id" : "466400200079875" } ], "paging" : { } } |
STEP 4 - Check your Access Token
Facebook Access Tokens will expire in about an hour. Access Token obtained with the above procedure are long-lived Access Tokens and will expire after two months (60 days).offline_access permission, used to prevent tokens expiration, was depreceted since 2012. Read more at https://developers.facebook.com/roadmap/offline-access-removal/.
Since offline access is deprecated you cannot create an access token which doesn’t expire. The best you can get is a long-lived Access Token (expires in 60 days).
So, it is very important to know your Access Token expiration time. Just use the URL
https://developers.facebook.com/tools/debug/access_token
For example, to check (debug) the Access Token obtained for this demo
(click
the
thumb for full image)
Notice that this Access Token will expire after 60 days.I remind you that this app (Demo_auto_post_php) has been deleted, so the above URL will not give you the same results. The above screenshot is taken before app deletion.
Renew Access Token after expiration
Facebook long-lived Access Tokens will expire after about two months (60 days). In order your app to remain functioning, you (unfortunately) have to repeat "How to Get Facebook Access Token - STEP 2".How to Find your Facebook id
The Facebook id of a Personal Profile of Fan Page or Business Page is public information, available using Facebook Graph API. Just use a URL like:http://graph.facebook.com/username
For example:
To find my Facebook Personal Profile id, use:
http://graph.facebook.com/chr.pontikis
To find Facebook id of my Fan Page (the Fan Page of this blog), use:
http://graph.facebook.com/pontikis.net
You may also use some online tools like http://findfacebookid.com/