Java 使用 RestFB api 发布到 Facebook 页面墙
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19491433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post to Facebook Page wall using RestFB api
提问by Alvin
I am trying to post on the wall of a facebook page. I am able to post on the user wall using App Access token.
我正在尝试在 facebook 页面的墙上张贴。我可以使用 App Access 令牌在用户墙上发帖。
I got the App Access Token through extending the DefaultFacebookClient
我通过扩展 DefaultFacebookClient 获得了应用程序访问令牌
public class ConnectionService extends DefaultFacebookClient{
public ConnectionService(String appId, String appSecret) {
AccessToken accessToken = this.obtainAppAccessToken(appId, appSecret);
this.accessToken = accessToken.getAccessToken();
}
}
With this I am able to post to user wall using the appID and appSecret. But when I tried to post to Page Wall i get error of " The user hasn't authorized the application to perform this action"
有了这个,我可以使用 appID 和 appSecret 发布到用户墙。但是当我尝试发布到页面墙时,我收到错误消息“用户尚未授权应用程序执行此操作”
Anyone can advice?
任何人都可以建议?
采纳答案by luschn
The App Access Token is the most basic one, and will not allow you to post anything. In order to post something to a Facebook Page (as a Page), you need to get a Page Access Token.
App Access Token 是最基本的,不允许您发布任何内容。为了向 Facebook 主页发布内容(作为主页),您需要获取主页访问令牌。
The process is a bit complicated, because you actually need to authorize the user with the "manage_pages" permission first, with the User Access Token you can call the API to get a Page Access Token (/me/accounts).
这个过程有点复杂,因为你实际上需要先给用户授权“manage_pages”权限,有了User Access Token就可以调用API获取Page Access Token(/me/accounts)。
See those links:
请参阅这些链接:
- https://developers.facebook.com/docs/facebook-login/
- https://developers.facebook.com/docs/facebook-login/access-tokens/
- http://www.devils-heaven.com/facebook-access-tokens/
- https://developers.facebook.com/docs/facebook-login/
- https://developers.facebook.com/docs/facebook-login/access-tokens/
- http://www.devils-heaven.com/facebook-access-tokens/
Btw, the REST API is deprecated: https://developers.facebook.com/blog/post/616/
顺便说一句,REST API 已弃用:https: //developers.facebook.com/blog/post/616/
You can also try the "Client Token" (Developer Settings > Advanced), i never worked with that one but it could be the easiest solution. In any case, an App Access Token is the wrong one.
您还可以尝试“客户端令牌”(开发人员设置 > 高级),我从未使用过该令牌,但它可能是最简单的解决方案。在任何情况下,应用程序访问令牌都是错误的。
回答by Jhanvi
回答by Bharath R
Since u r generating access token from java class. u can set the permissions u require from user in ur manage app link from ur facebook profile page and get the access token here.....
因为你是从java类生成访问令牌的。您可以在您的 Facebook 个人资料页面的管理应用程序链接中设置您需要的用户权限,并在此处获取访问令牌.....
回答by sufinawaz
To post on a facebook page wall, you will need to follow these steps:
要在 Facebook 页面墙上发帖,您需要按照以下步骤操作:
- Head over to https://developers.facebook.com/tools/explorer
- Click on "Get Access Token"
- Under "Extended Permissions" tab, select select manage_pages and publish_actions and hit "Get Access Token"
- Now under Graph API, under Get call, type in "me/accounts" and hit Submit
- In the screen below, you will see the "data" json object with all your pages and page access tokens.
- Grab the desired page token access and replace the PAGE_ACCESS_TOKEN in the code below with this token.
- Replace PAGE_NAME with your page name (the page name slug in the URL).
- Run the below code and that should do the job :)
- 前往https://developers.facebook.com/tools/explorer
- 点击“获取访问令牌”
- 在“扩展权限”选项卡下,选择 manage_pages 和 publish_actions 并点击“获取访问令牌”
- 现在在 Graph API 下的 Get call 下,输入“me/accounts”并点击 Submit
- 在下面的屏幕中,您将看到包含所有页面和页面访问令牌的“数据”json 对象。
- 获取所需的页面令牌访问权限并将下面代码中的 PAGE_ACCESS_TOKEN 替换为此令牌。
- 将 PAGE_NAME 替换为您的页面名称(URL 中的页面名称)。
- 运行下面的代码,这应该可以完成工作:)
final FacebookClient fb = new DefaultFacebookClient(PAGE_ACCESS_TOKEN);
final Page page = facebookClient.fetchObject(PAGE_NAME, Page.class);
facebookClient.publish("PAGE_NAME/feed", FacebookType.class, Parameter.with("message", "RestFB test"));
final FacebookClient fb = new DefaultFacebookClient(PAGE_ACCESS_TOKEN);
final Page page = facebookClient.fetchObject(PAGE_NAME, Page.class);
facebookClient.publish("PAGE_NAME/feed", FacebookType.class, Parameter.with("message", "RestFB test"));