Android Facebook SDK: does not send access token? - android

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.

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();

Facebook page access token with Android SDK

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());

Android Facebook check in with new 3.0 Facebook SDK

I could check in with the older Facebook SDK used this code:
Facebook fb = new Facebook("APP ID");
Bundle params = new Bundle();
params.putString("access_token", "TOKEN");
params.putString("place", "PLACE ID");
params.putString("message","SOMETHING MESSAGE");
JSONObject coordinates = new JSONObject();
coordinates.put("latitude", "LATITUDE");
coordinates.put("longitude", "LONGITUDE");
params.putString("coordinates",coordinates.toString());
params.putString("tags", "USER ID");
String response = fb.request("me/checkins", params, "POST");
But the fb = new Facebook() and the fb.request() functions are deprecated in new 3.0 Facebook SDK! What can I replace them with? How can I check in with using the 3.0 Facebook SDK?
The Graph API need "publish_checkins" extended permission in Facebook app, but this permission is deprecated also.
You should use the static methods of the Request class to create requests and execute them.
Looks like the most suitable method would be newStatusUpdateRequest that takes a GraphPlace as a parameter.
Alternatively, you can use the newPostRequest which allows you to construct a request that is much more similar to the way it was in previous SDK.

How to make my post to be seen by only some friends?

I have facebook ids of some of my friends. I want to post on my newsfeed,by using facebook graph api.
How can I make only these friends(facebook ids I have) to see my post?
Did I need to create any group for this?
Yes, it is possible. In the parameter bundle, add a privacy field. see example below:
public String postToWall() {
Bundle params = new Bundle();
params.putString("message", "test message");
JSONObject privacy = new JSONObject();
privacy.put("value", "CUSTOM");
privacy.put("friends", "SOME_FRIENDS");
privacy.put("allow", "FRIEND_ID_1,FRIEND_ID_2...");
params.putString("privacy", privacy.toString());
Facebook facebook = new Facebook(YOUR_APP_ID);
AsyncFacebookRunner mAsyncFacebookRunner = new AsyncFacebookRunner(facebook);
mAsyncFacebookRunner.request("me/feed", params, "POST", new BaseRequestListener() {
...
});
A post by its definition can be seen by everybody.
Alternatively you can
Post to a pre-created group
Message a group of friends

Android and Facebook: How to use Facebook Query Language (FQL) out of Android Application

On this page facebook tells you, that you can somehow use the Facebook-Query-Language: http://developers.facebook.com/docs/reference/fql/profile
But I just dont understand what the query looks like. On Stackoverflow I just find exapmles on how to use the FQL with PHP. But I want to use it out of and Android Application. Anybody can give me URLs or Code Examples?
Using the Facebook Android SDK, an FQL call can be implemented like this:
String query = "SELECT name FROM user WHERE uid = me()";
Bundle params = new Bundle();
params.putString("method", "fql.query");
params.putString("query", query);
mAsyncFacebookRunner.request(null, params, new FQLRequestListener());
where FQLRequestListener extends RequestListener, just as for a Graph call.
See this link https://developers.facebook.com/docs/reference/fql , and you can go deeper because you can click on each element and see all the propreties of each element , ( exemple "album" here : https://developers.facebook.com/docs/reference/fql/album/ ).
This is an exemple of query to get the url of the pictures of an album by "album_id" :
String fqlQuery = "SELECT src FROM photo WHERE album_object_id="+ <your_album_id>;
This is one of the ways to call your request :
Bundle params = new Bundle();
params.putString("format", "json");
params.putString("query", fqlQuery);
params.putString("access_token", fbAccessToken);
Session session = Session.getActiveSession();
Request request = new Request(session, "/fql",
params, HttpMethod.GET,
new Request.Callback() {
public void onCompleted(
Response response) {
Log.i(TAG, "Got results: "
+ response.toString());
}
});
Request.executeBatchAsync(request);
I hope I helped, good luck! :)
This seems to work if I put it into the Browser, but I dont know how to tell the Android Facebook SDK to use this query:
https://api.facebook.com/method/fql.query?query=select+pic_small+from+profile+where+id+%3D+USERID;&access_token=ACCESSTOKEN
I haven't tried it, but for calling fql you can try
public String request(String fql) throws FileNotFoundException, MalformedURLException, IOException {
Bundle parameters = new Bundle
parameters.putString("format", "json");
parameters.putString("query", fql);
parameters.putString("access_token", mFacebook.getAccessToken());
if (mFacebook.isSessionValid()) {
params.putString("access_token", mFacebook.getAccessToken());
}
String url = (fql != null) ? "https://api.facebook.com/method/fql.query" : "https://api.facebook.com/restserver.php";
return Util.openUrl(url, "GET", params);
}
It should work.
You have to take into acount that mFacebook is an authenticated Facebook Object (of the Facebook Android SDK). Also, that the fql sould be filled with a format supported by an url. For example:
select+pic_small+from+profile+where+id+%3D+USERID;
instead of
select pic_small from profile where id=USERID;

Categories

Resources