I'm trying to deal with Facebook SDK on Android. For now, posting to user's feed (me/feed) is done and working properly. After user authenticating, my application receives manage_pages permission, and propose user to save groups he have admin rights to.
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);
}
};
Later user can choose group to post. After that, application makes request to get page access token and starts posting:
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");
}
}
Here's executeRequest method:
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();
}
And callback for it:
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
}
}
};
And after post attempt i'm receiving error "The user hasn't authorized the application to perform this action". Please, help me to deal with correct request in this case. Or, maybe, i should try to make raw Graph curl request? I'm scratching out my head about 2 weeks with this, but the only information i finded is "you must use page access token". But how to use it correctly? Thanks.