How to get Page Access Token by code?

╄→尐↘猪︶ㄣ 提交于 2019-11-26 23:04:07

问题


I need to generate a Page Access Token for a webpage that I have, this token will be used by the webpage to post to its Facebook Page feed.

This is what I do :

  1. Go to Graph API Explorer
  2. Choose my app from the dropdown
  3. Click Get Access Token
  4. Choose correct permissions(manage_pages/user_events)

To get the Page Access Token I have to run me/accounts in the Graph API Explorer. This will show all the pages I got with a new Page Access Token (short lived(about 60 min)) to each one.

This Page Access Token will work just fine to use in my C# code to post to the feed.

Problem

When another user uses my webpage to post a post I need to generate a new Page Access Token(in code) and then use it to post the userpost to the site´s facebook feed. The problem is that I could clearly not be there to grant the post so how do I handle this?

I could obviously not run the me/accounts(in code) command to get the Page Access Token from here.

I have looked at extended Access Token but this will last 60 days and then it will be the same problem as above.


回答1:


What you need is an Extended Page Token:

  • generate User Access Token with manage_pages and publish_actions
  • generate Extended User Token (valid for 60 days): https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id=[app-id]&client_secret=[app-secret]&fb_exchange_token=[short-lived-token]
  • call /me/accounts with the Extended User Token to get Extended Page Tokens

Extended Page Tokens are valid forever, so you don´t need to generate a new one all the time.

Source (including information about all the Tokens): http://www.devils-heaven.com/facebook-access-tokens/




回答2:


On https://developers.facebook.com/docs/pages/access-tokens

Getting Page Access Tokens

GET /{page-id}?fields=access_token&access_token={user_access_token}

The response will look like this:

{
  "access_token": "{your-page-access-token}",
  "id": "{page-id}"
}


来源:https://stackoverflow.com/questions/25470403/how-to-get-page-access-token-by-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!