Location data is missing from GraphRequest (Android Facebook SDK) - android

Trying to have my Android app fetch a logged in Facebook user's e-mail, birthday, and location. The first two fields can be retrieved successfully, but location is missing from the Graph API's response, and I can't figure out why.
I'm logging in with these permissions:
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this, Arrays.asList("public_profile", "user_friends", "email", "user_birthday", "user_location"));
The GraphRequest is implemented as follows:
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.d("jsonTest171", object.toString(4));
String email = object.getString("email");
UserData.emailAddress = email;
String birthday = object.getString("birthday");
UserData.birthday = birthday;
String location = object.getJSONObject("location").getString("name");
UserData.location = location;
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,location,birthday");
request.setParameters(parameters);
request.executeAsync();
The Facebook user has the app set to allow all 3 requested fields, as shown here.
This is the LogCat output from the debug line included in the above code:
{
"id": "101936745620608",
"email": "test77124#gmail.com",
"birthday": "12\/20\/1983"
}
Why is the requested location field not present in the Graph response?

In stead of location ask for location{location} in the Bundle parameters like:
parameters.putString("fields", "id,email,birthday,location{location}");
The location part of the response looks like:
"location":{
"location":{
"city":"",
"country":"",
"latitude":0,
"longitude":0
},
"id":""
}
So you can retrieve them in your callback method like:
JSONObject location = object.getJSONObject("location").getJSONObject("location");
String city = location.getString("city");
String country = location.getString("country");

Related

facebook graph android sdk - under what circumstance will server response be null?

sometimes randomly a few of my users are getting null values from facebook graph request. but the only thing im requesting from the user is a email address ? lets say the usr did not have a email address but used a phone number to register with facebook. then still why would the raw server response be null. For most of my users its working. anyway, let me show you what my graph request looks like to shed some light:
GraphRequest request = GraphRequest.newMeRequest(
token,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject userInfo, GraphResponse response) {
if (userInfo == null)
Crashlytics.log("facebook graph info null:" + response.getRawResponse());
String email = "";
try {
email = userInfo.getString("email");
} catch (JSONException e1) {
e1.printStackTrace();
/* sometimes i end up in this catch block and the rawResponse object is null
}
presenter.presentPasswordPrompt(task, email, credential);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "email");
request.setParameters(parameters);
request.executeAsync();
as you can see i am printing out a log when the user info is null but the value from response.getRawResponse() is null itself so i cant figure out what the issue is.

Get All Photos from Facebook

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

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!

Can't get Email address through Facebook Android SDK

I tried to use below code to get my facebook email address via my app, but return value is null.
I don't know what the reason, but I can get email address using Graph API explorer. Is there anybody know what is wrong with this code?
#Override
public void onCreate(Bundle savedInstanceState) {
// Request email address
GraphRequest.newMeRequest(
loginResult.getAccessToken(),new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject me, GraphResponse response) {
Log.d("TEST", response.toString());
if (response.getError() != null) {
// handle error
} else {
Log.d("TEST", me.toString());
String email = me.optString("email");
String id = me.optString("id");
Log.d("TEST", "Email:" + email);
}
}
}).executeAsync();
LoginButton authButton =(LoginButton) findViewById(R.id.login_button);
List<String> permissions = new ArrayList<>();
permissions.add("public_profile");
permissions.add("email");
authButton.setReadPermissions(permissions);
}
Finally I found the solution, using newMeRequest method fetches fields from a user object. In the case, I need email as an additional fields, so added “email” into fields parameter and request specific fields:
GraphRequest request = GraphRequest.newMeRequest(
//...
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
Set the read permissions before you make the request and do it in the following way:
GraphRequestAsyncTask request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject user, GraphResponse response) {
final Profile profile = Profile.getCurrentProfile();
if ((user != null) && (profile != null)) {
accessToken = AccessToken.getCurrentAccessToken();
if (accessToken.getDeclinedPermissions().isEmpty()) {
try {
String email = user.get("email").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}).executeAsync();
You have to set permissions before the request.
Anywayt the email field may return null for two reasons:
1 - user didn't confirm the email address during the registration
2 - users could be signed up in Facebook using a phone number instead of email.
Documentation says:
Note, even if you request the email permission it is not guaranteed
you will get an email address. For example, if someone signed up for
Facebook with a phone number instead of an email address, the email
field may be empty.

Categories

Resources