我正在尝试在 Android 上处理 Facebook SDK。目前,发布到用户的提要(我/提要)已完成并正常工作。用户身份验证后,我的应用程序收到 manage_pages 权限,并建议用户保存他拥有管理员权限的组。
private void requestPermissions() {
List<String> permissions = session.getPermissions();
final List<String> PERMISSIONS = Arrays.asList("publish_actions",
"manage_pages");
if (!isSubsetOf(PERMISSIONS, permissions)) {
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
myActivity, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
}
};
以后用户可以选择组发布。之后,应用程序请求获取页面访问令牌并开始发布:
private void postToGroup() {
if (session != null) {
Log.d("FB", "groupToken request!");
Request myreq = Request.newGraphPathRequest(session, "me/accounts",
new Request.Callback() {
@Override
public void onCompleted(Response response) {
Log.d("FB", "received token");
GraphObject obj = response.getGraphObject();
JSONObject json = obj.getInnerJSONObject();
try {
String token = null;
JSONArray data = json.getJSONArray("data");
Log.d("FB", frag.getAcc().getLogin());
for (int i = 0; i < data.length(); i++) {
JSONObject acc = (JSONObject) data.get(i);
Log.d("FB", acc.getString("id"));
if (acc.getString("id").equals(
frag.getAcc().getLogin())) {
Log.d("FB",
"token finded! "
+ acc.getString("access_token"));
token = acc.getString("access_token");
}
}
Log.d("FB", token);
executeRequest(token);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
myreq.executeAsync();
} else {
Log.d("FB", "Session is closed");
}
}
这是 executeRequest 方法:
private void executeRequest(String token) {
postParams = new Bundle();
postParams.putString("name", postTitle);
postParams.putString("message", frag.getContent());
postParams.putString("access_token", token);
request = new Request(session, frag.getAcc()
.getLogin() + "/feed", postParams, HttpMethod.POST,
callback);
Log.d("FB", "Executing post request");
request.executeAsync();
}
并为其回调:
private Request.Callback callback = new Request.Callback() {
public void onCompleted(Response response) {
Log.d("FB", "get response!");
FacebookRequestError error = response.getError();
if (error == null) {
JSONObject graphResponse = response.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
Log.d("FB", postId);
} catch (JSONException e) {
Log.d("FB error", "JSON error " + e.getMessage());
}
} else {
Log.d("FB error", error.getErrorMessage()); //HERE I'M RECEIVING AN ERROR
}
}
};
在发布尝试后,我收到错误“用户尚未授权应用程序执行此操作”。在这种情况下,请帮助我处理正确的请求。或者,也许,我应该尝试提出原始 Graph curl 请求?我为此花了大约 2 周的时间,但我发现的唯一信息是“您必须使用页面访问令牌”。但是如何正确使用呢?谢谢。