And*_*ley 3 java android facebook facebook-graph-api
好吧,我已经想到了这个,但我想把它放在那里以防其他人遇到问题.基本上,我需要做的是发布Facebook用户拥有的页面(即我是John Doe,我是页面Rum Ham的管理员;我想发布到Rum Ham页面).
所以,基本上答案看起来像这样
首先,您需要使用此行记录用户
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions", "manage_pages", "publish_pages"));
Run Code Online (Sandbox Code Playgroud)
然后,您需要获取我们希望发布到的页面的访问令牌
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);
}
Run Code Online (Sandbox Code Playgroud)
好的,所以一旦我们获得了访问令牌页面,这里就是真正的f***kery发挥作用,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);
Run Code Online (Sandbox Code Playgroud)
现在,我们还没有完成.最后,我们需要实际进行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);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它.希望这有助于某人!
| 归档时间: |
|
| 查看次数: |
778 次 |
| 最近记录: |