20

I have a Node.js server running a social network site and I also have a facebook page for that site. For certain actions performed by users on my site, I want to post details on the facebook page of my app.

I referred to Thuzi facebook node sdk here on how to post to facebook wall. However, it requires app id, app secret and a temporary access token. App id and app secret are constant so I can put them somewhere in my config file and use from there. But how do I get the access token without any interaction from front-end ? All posts will be published by our app only and that too on our own page. I just want this to be triggered by the end user's actions. Any help ?

I am using Sails.js framework btw.

Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104

2 Answers2

32

You would need to use an Extended Page Token for that, you only need to create it once and it will stay valid forever. And you will post "as Page" with a Page Token. How to get an Extended Page Token:

  • Create an App
  • Use the Graph API Explorer to generate a User Access Token (by authorizing the App with the manage_pages and publish_actions permission)
  • Extend the User Access Token (valid for 60 days)
  • Request an Extended Page Token by calling /me/accounts

Store that Extended Page Token on your server and use it for posting on the Page wall.

Here are some additional resources, explaining everything in detail:

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • 1
    Thanks it worked! just a follow up. I will be posting the url containing some content from my site and I want it to appear as a card. How can I do that or facebook will do that automatically ? – Mandeep Singh Oct 28 '14 at 10:55
  • what do you mean with "card"? how stuff gets visible depends on many factors, if you post images always make sure to follow the guidelines: https://developers.facebook.com/docs/sharing/best-practices#images – andyrandy Oct 28 '14 at 11:25
  • For example, when I post a link to a youtube video, it does not get displayed just as a URL but as a card with the thumbnail of youtube video. I want to make sure that my link also gets displayed in a similar way and not just as a URL. Link will be pointing to an article on our website. – Mandeep Singh Oct 28 '14 at 11:38
  • check out the "link" field (with "picture" and stuff) in the facebook docs about the page feed. make sure you include that correctly. – andyrandy Oct 28 '14 at 11:57
  • it works a bit different with server side posting. if you share a link client side, you need to include open graph tags: http://ogp.me/ – andyrandy Oct 28 '14 at 11:58
  • Doing some hit and trial. Tried passing type as a parameter but does not work FB.api('me/feed', 'post', { message: encodeURI(link), type: "article" } , function (res) { if(!res || res.error) { console.log(!res ? 'error occurred' : res.error); return; } console.log('Post Id: ' + res.id); }); – Mandeep Singh Oct 28 '14 at 12:24
  • i thought you want to post on a facebook page, not a user profile? use /page-id/feed instead. – andyrandy Oct 28 '14 at 14:04
  • ...and i thought you want to do that server side, not client side? – andyrandy Oct 28 '14 at 14:04
  • I am doing it from server side only and the post will appear on the facebook page. I am still facing issues extending the access token. As per the documentation, if I request from oauth/access_token with grant_type fb_exchange_token, app id and app secret, it says that access token does not belong to this app. App id I am providing is not the graph api id but my App's id. If I provide app_id for Graph API, I don't know where to find app secret. Confused – Mandeep Singh Oct 28 '14 at 14:12
  • 1
    take a look at the third party overview: https://developers.facebook.com/docs/other-sdks – andyrandy Oct 28 '14 at 14:32
  • oh, my bad. i never used the facebook api with node.js, it does look like the client side javascript sdk - at least one of them. – andyrandy Oct 28 '14 at 14:33
  • anyway, you only need to provide the extended page access token for the api calls. no need to use app id and app secret if you store the page token. – andyrandy Oct 28 '14 at 14:34
  • Just a quick question. Would extended page access token expire when user access token (used to obtain page access token) expires? – kestutisb Nov 11 '17 at 15:20
0

I am also digging more in to this nowdays As I am working on a node module for this.

Till now I got to know that we can create a temporary access_token and we can than extend that token upto max 60 days.

For this after getting temporary token you need to make a call to this url to get a access token with 60 days validity.

https://graph.facebook.com/oauth/access_token?client_id=&client_secret=&grant_type=fb_exchange_token&fb_exchange_token=