Generate a #Facebook #AccessToken that never expires
I thought this was a very useful step by step guide to generating an access token in Facebook that never expires:
Steps for Creating Never Expire Page Access Token.
- Go To link https://developers.facebook.com/
- Login with your facebook Credential
- Go to Right upper Menu My Apps → Select Add New App / Create New App
- After Creating a New App you have redirect to App Dashboard page
- Click on Dashboard you see App Id and App Secret
- Now You Go to App Review from the left menu. Make App Public.
- Now you go to Tools and Support menu
- Select Graph API Explorer
- Now at Right Upper Corner. Select Your Application. By default, “Graph Api Explorer” selected
- Now Click on Get Token Button. And then you see three option.
- Select “Get User Access Token” Popup open, Select manage_pages and publish_pages Checkboxes and click on “Get Access Token” Button.
- Now Again Click on Get Token Button→ Select Page Access Token→ choose your Page.
- After Selecting your page your short term Access token is generated. That is in the Access token textbox.
- Now Click Information button(blue i button) that is in left side near the Access Token label. One poupu open, now click on “Open in Access Token tool”
- Another tab is open. Now Click on Extend Access Token. Two month Access Token is generated
- Now go to previous tab Graph Api Explorer paste this extended token in the Access token toolbox.
- Add one parameter in “access_token” in the get toolbox so final string looks like “me?fields=id,name, access_token”
- Click on submit button. Your never expire token is generated in the response
- Copy this and check this access token paste on Access Token debugger.
And to finish off, here’s a code example in C# to post to a Facebook business page:
static async Task PostToFacebookAsync(string accessToken, string pageId)
{
// Use HttpClient
using (var client = new HttpClient())
{
// Set variable values for post to facebook
string uri = string.Format(“https://graph.facebook.com/{0}/feed?”,pageId);
var strName = HttpUtility.UrlEncode(result.Title + suffix);
var strLink = HttpUtility.UrlEncode(result.MediaUrl);
// Formulate querystring for graph post
StringContent queryString = new StringContent(“access_token=” + accessToken + “&message=Hello+World”);// Post to facebook /{page-id}/feed edge
HttpResponseMessage response = await client.PostAsync(new Uri(uri), queryString);
if (response.IsSuccessStatusCode)
{
// Get the URI of the created resource.
string postId = await response.Content.ReadAsStringAsync();
Console.WriteLine(“Post Successfully: Your Post ID is: {0}”,postId);}
else
{
Console.WriteLine(“Something Went Wrong”);}
}
}