Facebook page access token with Android SDK - android

I'm able to login my user using the LoginManager in the Android Facebook SDK and I get an access token.
Than, I retrieve the list of pages for which he is admin, and I can get the access token string for the page chosen by the user,
My objective is to allow the user to post as page but I can't create a valid AccessToken object for this.
I'm trying:
public void postMessage(String message, String pageId, FragmentActivity activity, GraphRequest.Callback callback, Context context) {
Bundle params = new Bundle();
params.putString("message", message);
AccessToken accessToken = AccessToken.getCurrentAccessToken();
if ( !pageId.equals("me")) {
accessToken = new AccessToken(SettingsManager.getInstance(context).getFacebookAccessTokenPageId(), context.getString(R.string.app_id), pageId, null, null, null, null, null);
}
GraphRequest request = new GraphRequest(accessToken, pageId + "/feed", params,
HttpMethod.POST, callback);
request.executeAsync();
}
But when I send the post I get an error. If I use the currentAccessToken without trying to force the page access token, I can post the message in the page but as normal user and not as page.
Does anybody know how to create an access token to be able to post as a page?
Thanks a lot
Emanuele

you can get this access token from Graph API explorer, the link given below...
https://developers.facebook.com/tools/explorer/
log in with your account the page you admin and get the access token from here, and put it into your code.
or initialize the new access token in code with the help of this token..
AccessToken token = new AccessToken("YOUR_TOKEN_HERE_FROM_EXPLORERE",
AccessToken.getCurrentAccessToken().getUserId(),
AccessToken.getCurrentAccessToken().getUserId(), Arrays.asList("publish_actions", "manage_pages", "publish_pages"),
null, AccessTokenSource.FACEBOOK_APPLICATION_SERVICE,
AccessToken.getCurrentAccessToken().getExpires(), AccessToken.getCurrentAccessToken().getLastRefresh());

Related

Facebook Graph API not returning Gender

I am using Facebook SDK to sign-in a user. To get the gender I have added user_gender permission in the permission list.
List<String> accessPermissions = Arrays.asList("user_gender", "email", "user_birthday");
LoginManager.getInstance().logInWithReadPermissions(activity, accessPermissions);
After which I get the access token. Now I am passing this access token to the backend team. And the backend team is using Facebook Graph API to get the user details like birthday, gender, email, etc.
They hit the following API to get user details.
https://graph.facebook.com/me?access_token=dummyaccesstoken&fields=name,gender,birthday,email
They are able to get details like email and birthday. But they are not able to get the gender of the user. Since it is a GET API, I can also see that we are not getting the gender of the user.
However, if we try to get the details in Android itself instead of passing the access token to the backend, then it works fine.
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
Log.d("Response", "response is "+response.getJSONObject().toString());
// Application code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link, gender");
request.setParameters(parameters);
request.executeAsync();
So what is the issue in the Facebook Graph API? Why is it not able to return the gender? Anyone else faced the issue?
List<String> accessPermissions = Arrays.asList("user_gender", "email", "user_birthday");
LoginManager.getInstance().logInWithReadPermissions(activity, accessPermissions);
write instead of user_gender and email and user_birthday
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
and write the Bundle request params
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday");
request.setParameters(parameters);
request.executeAsync();

Android Facebook SDK: How to retrieve Mutual friends Graph API response

I implemented this code from the Facebook SDK to get mutual friends.
Bundle params = new Bundle();
params.putString("fields", "context.fields(mutual_friends)");
// make the API call
new Request(
session,
"/{user-id}",
params,
HttpMethod.GET,
new Request.Callback() {
public void onCompleted(Response response) {
/* handle the result */
Log.d("results", response.toString());
}
}
).executeAsync();
How do i read the Response object and retrieve the total count and the list of mutual friends? I have successfully gotten the result in the log.
Thanks.
Booth people need autorize your friend_list_permission to you see the users in common. If not you need use all_mutual friends, but you just can run this in server, no working in clients. look documentation in all_mutual_friends.
I explain this post here.
https://stackoverflow.com/a/34663235/3516549

get facebook graph user from Respone

I am new to facebook 3.0 sdk so sorry in advance. My problem is this:
I am trying to get user id, in order to do so I found out that I need to do this:
Session session = Session.getActiveSession();
Request request = Request.newMeRequest(session, null);
Response response = Request.executeAndWait(request);
from here I am stacked, I don't know how to get the Id of the user from the response.
Maybe there is some Way to request the GraphUser?
Thanks!
According to the documentation, you can use http request on
https://graph.facebook.com/me?fields=id&access_token=replace-this-by-your-access-token
to get the id of the logged in user
You can do this:
Request request = new Request(session, "me", null, HttpMethod.GET);
Response response = request.executeAndWait();
if (response.getError() != null) {
// bla bla
} else {
GraphUser graphUser = response.getGraphObjectAs(GraphUser.class);
}

Unable to execute "Facebook Like" from within Android application

From within my Android application, i am trying to implement a simple "Like Button" functionality.
We have already executed Facebook log in from the same app.
We have also created an Open Graph Like action & a Link object as well on our App's Dashboard.
This is the code that we are using to publish the like.
public static void likeFacebookPage(Context context,String pageUrl)
{
Bundle params = new Bundle();
params.putString("object",pageUrl);
Request request = new Request(
Session.getActiveSession(),
"me/og.likes",
params,
HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response
}
In the pageUrl parameter, we have:
http://www.facebook.com/<object_id>
"object_id" is the Id of the Link Object that we have created in Open Graph.
After executing the above method, the response object received as follows:
{Response: responseCode: 200, graphObject: null, error: {HttpStatus: -1, errorCode: -1, errorType: null, errorMessage: null}, isFromCache:false}
But the Like count is not getting updated, leading us to believe that this process is not working.
We have also included the permissions "publish_actions" & "user_likes" in our code as well as on the App Dashboard.
Kindly help us with configuring this functionality.
Thanks in advance.
As described here https://developers.facebook.com/docs/reference/api/publishing/ you publish a like to an object like so with the publish_actions and publish_stream permissions:
Bundle postParams = new Bundle();
Request request = new Request(Session.getActiveSession(),
FB_OBJECT_ID+"/likes", postParams, HttpMethod.POST, new Callback() {
#Override
public void onCompleted(Response response) {
}
});
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
Where your FB_OBJECT_ID is simply the ID of the object. I.e if you want to like a photo, FB_OBJECT_ID would be the pid. For an album, FB_OBJECT_ID would be the aid.

Android Facebook SDK: does not send access token?

I'm using the following snippet to request the friendslist of the authorized user:
System.out.println("AT: " + facebook.getAccessToken());
String response = facebook.request("me/friends?fields=first_name,last_name,id,gender");
The response I get:
{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
I'm printing the accesstoken, this gives me a valid token. Also, getAccessExpires() returns a time in the future. When I request the url "me/friends" without any params, I get the expected (but with less data) friends list without errors.
How can I solve this?
String response = facebook.request("me/friends?fields=first_name,last_name,id,gender");
That is the incorrect way to use facebook.request. I'm assuming you're not using the beta Android SDK so the correct way to do this is to do the following:
Facebook facebook = new Facebook(YOUR_APP_ID);
AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
Bundle params = new Bundle();
params.put("fields", "first_name, last_name, id, gender");
mAsyncRunner.request("me/friends", params, new RequestListener() {
// get response here
....
});
Hope this helps.

Categories

Resources