Can't get access token for a facebook page for an application that has all the right permissions

早过忘川 提交于 2019-12-04 18:22:51

We just managed to get this working in this past couple of days. Here's a summary. I apologize if I am detailing it beyond necessary but a couple of things you have done seem a bit wrong. To sum up your steps:

If you are doing step_A correct, which by the looks of the URL seems all right, then at the end of it you will receive a CODE (not the final access token). This will be a redirection using the redirect URL you sent in step A, so make sure your server is accepting a request for that redirectURL specified there. It will be in the form http://redirectURL?code=A_CODE_GENERATED_BY_SERVER so doing params[code] should get you the CODE.

Step_B might be where you are slightly off. You have send the CODE from step_A back to the server. I see that you have set a scope parameter again which is now not necessary (it was done in step_A). Step_B your request URL should look like

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

This time Facebook sends back a Response not using the redirectURL though. If you have the correct Step_B request URI you can paste it in your browser location bar and the response will render in your browser itself. You can use an HTTPBuilder (that's what I am using) and capture this Response body. It is of the form access_token=<really_big_string>&expires=<time_to_live>. Parse this response whichever way you prefer.

This is also an access_token (let's call it access_token_uno) and I suppose there is some amount of things you can do using this. I haven't tried it myself though and so we proceed to Step_C. I suppose you already know your PageID. You have to send back access_token_uno to Facebook in a URL of the form

https://graph.facebook.com/<PageID>?fields=access_token&access_token=<access_token_uno>

The response will be a JSON block of the form:

{
   "access_token": <required_page_access_token>,
   "id": <PageID>
}

Parse the JSON block and you are done.

The one minor detail to remember: The redirectURL has to remain the same in Step_A and Step_B else the thing fails. Also, I haven't yet tried with the offline_access permission but I don't think the steps above will change even if that is the case.

You need to get https://graph.facebook.com/{userid}/accounts for a user with administrative rights to the page. Inside that you should find the access_token for the page.

http://developers.facebook.com/docs/authentication/#app-login

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