Java Facebook SDK 4.x Android发布为Facebook页面
好的,我已经弄明白了,但我想把它放在那里,以防其他人遇到问题。基本上,我需要做的是将帖子作为Facebook用户拥有的页面(即,我是John Doe,我是page Rum Ham的管理员;我想发布到Rum Ham页面) 所以,基本上答案是这样的 首先,您需要使用此行登录用户Java Facebook SDK 4.x Android发布为Facebook页面,java,android,facebook,facebook-graph-api,Java,Android,Facebook,Facebook Graph Api,好的,我已经弄明白了,但我想把它放在那里,以防其他人遇到问题。基本上,我需要做的是将帖子作为Facebook用户拥有的页面(即,我是John Doe,我是page Rum Ham的管理员;我想发布到Rum Ham页面) 所以,基本上答案是这样的 首先,您需要使用此行登录用户 LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions", "manage_pages"
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);
}
现在,我们还没有完成。最后,我们需要实际调用API来创建post:
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);
}
}
差不多就是这样。希望这对某人有帮助
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);
}
}