好的,我已经理解了这个问题,但是为了帮助其他遇到同样问题的人,我想分享一下解决方法。基本上,我需要让帖子以Facebook用户拥有的页面身份发布(例如:我是John Doe,我是Rum Ham页面的管理员,我想要在Rum Ham页面发布帖子)。
好的,我已经理解了这个问题,但是为了帮助其他遇到同样问题的人,我想分享一下解决方法。基本上,我需要让帖子以Facebook用户拥有的页面身份发布(例如:我是John Doe,我是Rum Ham页面的管理员,我想要在Rum Ham页面发布帖子)。
所以,基本上答案看起来像这样
首先,您需要使用这行代码登录用户
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions", "manage_pages", "publish_pages"));
然后,您需要获取我们希望发布到的页面的访问令牌
Bundle params = new Bundle();
//ok so access token here is "app_ID|app_secret"
params.putString("access_token", accessToken);
params.putString("fields", "id");
GraphRequest request = new GraphRequest(null, "me", params, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
JSONObject values = response.getJSONObject();
try {
//so I have to get the user ID first
String id = values.getString("id");
Bundle p = new Bundle();
p.putString("access_token", accessToken);
//yay nest the graph requests
//once we get the id we can get their pages
GraphRequest pagesRequest = new GraphRequest(null, "/" + id + "/accounts", p, HttpMethod.GET, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
//ok so here, we're getting the pages back in a few JSON wrappers
JSONObject values = response.getJSONObject();
JSONArray array = null;
try {
array = values.getJSONArray("data");
//ok, so here we're just iterating through the pages a user has, obviously you can handle this accordingly..
for (int i = 0; i < array.length(); i++) {
//ok, here's how to actually get the token
String access_token = array.getJSONObject(i).getString("access_token")
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
GraphRequest.executeAndWait(pagesRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
GraphRequest.executeAndWait(request);
}
好的,一旦我们得到了访问令牌页面,这就是Facebook在他们的参考页面中拒绝告诉你的真正问题所在。所以,请忘记任何你读过的需要向他们提交应用程序进行审核的内容。我所要做的就是创建一个新的访问令牌就像这样:
//create a new access token, facebook refers to this as a page access token
AccessToken token = new AccessToken("page_access_token", AccessToken.getCurrentAccessToken().getUserId(), Arrays.asList("publish_actions", "manage_pages", "publish_pages"), null, AccessTokenSource.FACEBOOK_APPLICATION_SERVICE,
AccessToken.getCurrentAccessToken().getExpires(), AccessToken.getCurrentAccessToken().getLastRefresh());
//then we simply update our current access token
AccessToken.setCurrentAccessToken(token);
现在,我们还没有完成。最后,我们需要实际调用API来创建帖子:
Bundle params = new Bundle();
params.putString("message", "Contents of message");
//here, token is our newly created AccessToken object
GraphRequest request = new GraphRequest(token, "/pageid/feed", params, HttpMethod.POST, new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
FacebookRequestError error = response.getError();
if (error != null) {
Log.e("Error", error.getErrorMessage());
} else {
//do your own processing here for success
}
}
});
GraphRequest.executeAndWait(request);
}
}
就是这样了。希望这对某人有所帮助!