无法获取具有所有正确权限的应用程序的 Facebook 页面的访问令牌
SO 社区,
我陷入了尝试将应用程序自动发布到我的 Facebook 页面的过程中。
以下是我采取的步骤:
A) 我为我的所有 Facebook 页面授权了我的应用程序,并使用离线访问、发布流和管理页面授予了权限
https://www.facebook.com/dialog/oauth?client_id=<app_id>&redirect_uri=<url>&scope=read_stream,publish_stream,manage_pages,offline_access,publish_actions
B) 我为具有这些权限的应用程序请求了访问令牌
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=<app_id>&client_secret=<app_secure_key>&scope=manage_pages,offline_access,publish_stream
C) 我现在想要获取访问令牌让页面能够使用
https://graph.facebook.com/feed?<page_access_token>&message=test&id=<page_id>&method=post
但是获取 page_access_token 来完成最后的帖子这是我失败的地方
这是我正在尝试的调用
https://graph.facebook.com/<page_id>?fields=access_token&access_token=<app_access_token>
,但我只是得到这个而不是页面的访问令牌:
{
"id": "<page_id>"
}
任何人都知道我缺少什么得到。
/托马斯
SO community,
I am stuck in my attempt to have an application post automatically to my Facebook page.
These are the steps I took:
A) I authorized my application for all my facebook pages and granted permissions offline_access, publish_stream and manage_pages using
https://www.facebook.com/dialog/oauth?client_id=<app_id>&redirect_uri=<url>&scope=read_stream,publish_stream,manage_pages,offline_access,publish_actions
B) I requested the access token for the application with those permissions
https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=<app_id>&client_secret=<app_secure_key>&scope=manage_pages,offline_access,publish_stream
C) I now want to get the access token for the page to be able to do the final post using
https://graph.facebook.com/feed?<page_access_token>&message=test&id=<page_id>&method=post
however getting the page_access_token is where I am failing
This is the call I am trying
https://graph.facebook.com/<page_id>?fields=access_token&access_token=<app_access_token>
but instead of the page's access token I just get this back:
{
"id": "<page_id>"
}
Anyone has any insight what I am missing to get the .
/Thomas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在过去的几天里,我们刚刚成功完成了这项工作。这是一个总结。如果我的细节超出了必要范围,我深表歉意,但你所做的一些事情似乎有点错误。总结一下您的步骤:
如果您正确执行了 step_A(从 URL 的外观来看似乎没问题),那么最后您将收到一个 CODE(不是最终的访问令牌)。这将是使用您在步骤 A 中发送的重定向 URL 进行的重定向,因此请确保您的服务器正在接受对此处指定的 redirectURL 的请求。它将采用
http://redirectURL?code=A_CODE_GENERATED_BY_SERVER
的形式,因此执行 params[code] 应该可以获取代码。Step_B 可能是你稍微偏离的地方。您已将步骤_A 中的代码发送回服务器。我发现您再次设置了
scope
参数,但现在没有必要了(它已在 step_A 中完成)。 Step_B 您的请求 URL 应类似于“这次 Facebook 发回
响应
”,但不使用重定向URL。如果您有正确的 Step_B 请求 URI,您可以将其粘贴到浏览器位置栏中,响应将在您的浏览器本身中呈现。您可以使用 HTTPBuilder (这就是我正在使用的)并捕获此响应正文。它的格式为access_token=&expires=
。以您喜欢的方式解析此响应。这也是一个access_token(我们称之为
access_token_uno
),我想您可以使用它做一些事情。不过我自己还没有尝试过,所以我们继续Step_C。我想您已经知道您的PageID
。您必须以以下形式的 URL 将 access_token_uno 发送回 Facebook响应将是以下形式的 JSON 块:
解析 JSON 块,您就完成了。
需要记住的一个小细节:Step_A 和 Step_B 中的重定向URL 必须保持相同,否则操作会失败。另外,我还没有尝试使用
offline_access
权限,但我认为即使是这种情况,上述步骤也不会改变。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 likeThis 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 formaccess_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 yourPageID
. You have to send back access_token_uno to Facebook in a URL of the formThe response will be a JSON block of the form:
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.您需要为具有管理权限的用户获取 https://graph.facebook.com/{userid}/accounts该页面的权利。在其中您应该找到该页面的 access_token。
http://developers.facebook.com/docs/authentication/#app-login
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