Get All Photos from Facebook - android

I need to get user uploaded and tagged photos from facebook account using graph API.
I tried following code but it's give nothing to me .
GraphRequest request = GraphRequest.newMeRequest(
token,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// Insert your code here
Log.e("Hi "," "+response.getJSONObject().toString());
}
});
GraphRequest req = GraphRequest.newGraphPathRequest(token, "me/photos", callback);
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name");
req.setParameters(parameters);
req.executeAsync();
I have given following permission to facbook.
loginButton.setReadPermissions(Arrays.asList("public_profile","user_photos"));
Thanks

Use this API
json.FB_URL+json.PAGE_ID+"?fields=albums.limit(30)%7Bname%2Cid%2Cdescription%2Cpicture%7D&access_token="+json.ACCESS_TOKEN

Related

Facebook Android SDK response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT) returns null

I'm using Facebook's Android SDK to retrieve information about the user.
My initial request is the following:
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(), this);
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,events");
request.setParameters(parameters);
request.executeAndWait();
and this works fine. The callback looks as follows:
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
User user = parseAndValidateFacebookResponse(object);
if (user.getEvents().getData().size() != 0) {
// do stuff here
}
GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
if (nextRequest != null) {
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,events");
nextRequest.setParameters(parameters);
nextRequest.setCallback(this);
nextRequest.executeAsync();
}
}
I'm able to parse the JSONObject, and when I inspect the raw response, it looks the same as what Facebook's Graph API Explorer gives me.
Part of the JSON (in the raw GraphResponse as well as in the Graph Explorer) describes the pagination:
"paging": {
"cursors": {
"before": "TVRRek1qTTJOREF3TXpZAMU1qQTFNRG94TmprNE5URTJNREF3T2pFNU9UZAzFPREF4TXpNNU9EUXdPQT09",
"after": "TVRVM05qYzVNVEkxTlRrMU1ERTJNam94TkRjME1EVTVOakF3T2pFNU9UZAzFPREF4TXpNNU9EUXdPQT09"
},
"next": "https://graph.facebook.com/v2.8/customerID/events?access_token=accesstoken"
}
However, when I try to fetch the next GraphRequest as part of the GraphResponse, I always get back null.
What am I doing wrong?
I'm not sure if this is related to the fact that I was using a newMeRequest instead of a "regular" GraphRequest, but I got this working after changing my implementation to the following:
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,events");
parameters.putString("limit", "25");
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/" + Profile.getCurrentProfile().getId() + "/events",
parameters,
HttpMethod.GET,
this)
.executeAsync();
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
User user = parseAndValidateFacebookResponse(object);
if (user.getEvents().getData().size() != 0) {
// do stuff here
}
GraphRequest nextRequest = response.getRequestForPagedResults(GraphResponse.PagingDirection.NEXT);
if (nextRequest != null) {
Bundle parameters = new Bundle();
nextRequest.setParameters(parameters);
nextRequest.setCallback(this);
nextRequest.executeAsync();
}
}
Right now I'm able to traverse through the next pages, using getRequestForPagedResults.

Set Facebook graph Request to always return English results

The language of the results returned depends on the settings on a users phone and/or their location. I want to force the request to only return English results.
The request in question is just a simple "me" request for a user.
So far I have tried to add "local" in the parameteres like this:
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
}
});
Bundle param = new Bundle();
param.putString("fields", "albums, id, birthday, photos&locale=en_us");
graphRequest.setParameters(param);
graphRequest.executeAsync();
I have also tried to do it like this:
String graphPath = "/me?fields=id,name , first_name,albums, last_name&locale=en_us";
GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), graphPath, null, HttpMethod.GET, new GraphRequest.Callback() {
#Override
public void onCompleted(GraphResponse response) {
Log.i(TAG, "response: " + response);
Log.i(TAG, "object: " + response.getJSONObject());
}
});
GraphResponse response = request.executeAsync();
Both without any luck. Last method actually works in the Facebooks Graph Api Explorer: https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=me%3Ffields%3Dalbums%26locale%3Dnb_no&version=v2.5
Without adding the "&locale=en_us" the top graph request works as intended.
It was actually a bug in the Facebook sdk version I had, so it would not return only english results. It works to set "locale" as an extra parameter. The correct way to do it is:
GraphRequest graphRequest = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
}
});
Bundle param = new Bundle();
param.putString("fields", "gender, email, name, albums");
param.putString("locale", "en_US");
graphRequest.setParameters(param);
graphRequest.executeAsync();
If this does not work for you, try to upgrade your Facebook sdk. I'm now on version 'com.facebook.android:facebook-android-sdk:4.11.0' and this works perfectly.

How to set different "limit" parameters for feed and likes in Android Facebook SDK?

I am trying to implement a method in Android by using Facebook SDK to make a GraphRequest to have the feed of the currently logged in user.
public void getMyStatusInformation(AccessToken accessToken){
GraphRequest graphRequest = new GraphRequest(
accessToken,
"/me/feed",
null,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
JSONObject responseObject = response.getJSONObject();
try {
onStatusesReceived(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "likes,story");
parameters.putString("limit", "2");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
I want to get only last 2 posts of the user, but get all the likes of his/her friends to those posts. However, when I execute this GraphRequest, the response contains 2 posts with maximum number of 2 likes.
I tried /me/feed?limit=2, had address not found error.
Tried
parameters.putString("fields", "likes,story.limit(2)"); had the error "Story parameter does not support subfield limit"
Changed the order as:
parameters.putString("limit", "2");
parameters.putString("fields", "likes,story");
But nothing changed.
How can I do this?

Facebook Android SDK — no 'email' in meRequest

I'm using the following code to get user's email:
AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
// Obtaining
}
}).executeAsync();
The following is checked via debugger:
AccessToken instance contains permissions "email" and "public_profile"
AccessToken instance declined permissions set is empty
Resulting JSONObject contains only user id and name, but no email
Profile has email — I'm authenticating via email.
I'm using Facebook SDK 4.5
EDIT: Request via direct HTTP graph api call returns email. But Android SDK does not.
How to fix it?
Try this out, you need to send "email" as part of parameters for GraphRequest.
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
I think the user can specify no email permissions when he/she signs in via facebook. So even if you expect it, you might not get the email address.
Maybe we facing same problem, try this :
Bundle params=new Bundle();
params.putString("fields","id,name,email,verified"); //must specify the "fields" param, otherwise FB always return id and name only.
new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me",
params,
HttpMethod.GET,
new GraphRequest.Callback() {
public void onCompleted(GraphResponse response) {
//do something...
}
}
).executeAsync();

How to get user email and birthday from Facebook API Version v2.4

I am not able to get the facebook user email in API version v2.4. I have one app in api version2.3 it returns the email and other details of the user but now facebook updated the api version to new application. if i use the old application like veriosn <=2.3 its returning the user email by using the following code. But in api version 2.4 i am not able to get the email and date of birth.
GraphRequest.newMeRequest(result.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
{
#Override
public void onCompleted(JSONObject me, GraphResponse response)
{
if (response.getError() != null)
{
// handle error
}
else
{
Log.e("",""+me.toString());
Log.e("",""+response.getJSONObject().toString());
}
}
}).executeAsync();
You'll have to explicitly define which fields you want to retrieve with your request, for example /me?fields=id,name,email,birthday instead of just /me
See
https://developers.facebook.com/docs/apps/changelog#v2_4
To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.
Since v2.4 you need include in your request the fields that you need.
GraphRequest request = GraphRequest.newMeRequest(
AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject userMe, GraphResponse response) {
if(userMe!=null){
//...... do your things
}
}
});
Bundle parameters = new Bundle();
//Add the fields that you need, you dont forget add the right permission
parameters.putString("fields","email,id,name,picture,birthday");
request.setParameters(parameters);
//Now you can execute
GraphRequest.executeBatchAsync(request);
Here is code block that worked for me:
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
final JSONObject jsonObject = response.getJSONObject();
try{
email = jsonObject.getString("email");
first_name = jsonObject.getString("first_name");
last_name = jsonObject.getString("last_name");
gender = jsonObject.getString("gender");
birthday = jsonObject.getString("birthday");
} catch (JSONException e){
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email,first_name,last_name,gender,birthday");
request.setParameters(parameters);
request.executeAsync();
Hope this helps!

Categories

Resources