Facebook graph api not sending all data in android - java

I'm trying to get user_id, name & email from facebook via graph API. but its not sending me the email. I'm using a function like this:
void callGraphApi() {
accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
tv_response.setText(response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email");
request.setParameters(parameters);
request.executeAsync();
}
I'm only getting a response like this:
{"id":"1480750682018443","name":"Ogwemuvwem Ossas","gender":"male"}, error: null}
Any solution??

In your facebook developer dashboard Go to App review in There you can see your data request permission.
If email permission not listed there you have to add it by click start a submission button on the same page.
From facebook Docs:
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.

You can try this new api. You can pass permissions in ArrayList like this.
loginButton.setReadPermissions(Arrays.asList("public_profile", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
getUserDetails(loginResult);
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
You can handle LoginResult like this.
// Get Facebook login results
protected void getUserDetails(LoginResult loginResult) {
GraphRequest data_request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject json_object, GraphResponse response) {
Log.i("onfbCompleted: ", json_object.toString());
}
});
Bundle permission_param = new Bundle();
permission_param.putString("fields", "id,name,email,picture.width(120).height(120)");
data_request.setParameters(permission_param);
data_request.executeAsync();
}
I Hope it's help for you. :)

Related

Can I get Contact Number while login in app using Facebook along with other fields like fbUserName, fbEmail,fbPicture etc.?

I want to fetch the Contact Number using facebook login in my app, is there any way to fetch the contact number ??
I have to show the facebook user details in my app and if the user hasn't added his contact Number then I simply set the ContactNo column as empty where I want to show the basic user details.
I have fetched the user details like this:
//useLoginInformation() method is called from the CallbackManager listener to display details once the user has successfully logged in
private void useLoginInformation(AccessToken accessToken) {
/**
Creating the GraphRequest to fetch user details
1st Param - AccessToken
2nd Param - Callback (which will be invoked once the request is successful)
**/
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
//OnCompleted is invoked once the GraphRequest is successful
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
String name = object.getString("name");
String email = object.getString("email");
String id=object.getString("id");
String image_url="https://graph.facebook.com/" + id + "/picture?type=normal";
//Send facebook user details from login activity to FacebookUserDetails activity
Intent intent=new Intent(LoginActivity.this,FacebookUserDetails.class);
intent.putExtra("facebookUsername",name);
intent.putExtra("facebookUserEmail",email);
intent.putExtra("facebookPic",image_url);
startActivity(intent);
progressBar.setVisibility(View.VISIBLE);
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
});
// We set parameters to the GraphRequest using a Bundle.
Bundle parameters = new Bundle();
parameters.putString("fields", "name,email,id,picture");
request.setParameters(parameters);
// Initiate the GraphRequest
request.executeAsync();
}

How to get Facebook information in android

I am trying to get information from my facebook in android ,
my code :
loginButton.setReadPermissions(Arrays.asList("email", "user_photos", "public_profile"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(),""+loginResult.getAccessToken(),Toast.LENGTH_SHORT).show();
String accessToken = loginResult.getAccessToken().getToken();
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.i("LoginActivity", response.toString());
// Get facebook data from login
try {
object.get("gender");
object.get("email");
} catch (JSONException e) {
e.printStackTrace();
}
// Bundle bFacebookData = getFacebookData(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, first_name, last_name, email,gender, birthday, location"); // ParĂ¡metros que pedimos a facebook
request.setParameters(parameters);
request.executeAsync();
}
But the program does not get into `public void onCompleted(JSONObject object, GraphResponse response) {
the result of this function is :
{Request: accessToken: {AccessToken token:ACCESS_TOKEN_REMOVED permissions:[email, user_photos, public_profile]}, graphPath: me, graphObject: null, httpMethod: GET, parameters: Bundle[{}]}
Is there any solution? or this case related to facebook privacy settings ?
Try the building your login flow manually. This way you don't get stuck on the sdk.
https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
Try replacing
loginResult.getAccessToken()
with
loginResult.getAccessToken().getToken() .
Facebook doesn't allow devs to Log "session.getAccessToken" directly, because it may cause leaks.Can also check this for more information: http://stackoverflow.com/a/29544390/2754871

Facebook Login - get user data in onCancel

I added Facebook login button to my code. it works well.
Assuming that the user logs in via facebook and unchecks the 'user_friends' permission: 'onSuccess' function will be called so the user data can be taken from the loginResult.
Next time when he opens the app, he will get the facebook permission screen in order to allow the 'user_friends' permission. Let's say that he unchecks it again: 'onCancel' function will be called although the user will be now logged in automatically as expected (because he has never logged out).
How can I get his data now in onCancel? (because I know that he is logged in but unchecked some permission)
The User logged in last time and not this time so onSuccess won't be called this time
mCallbackManager = CallbackManager.Factory.create();
mLoginButtonFacebook.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
}
#Override
public void onCancel() {
AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
// here I would like to retrieve the user data
// the user is logged in with canceled permissions
}
}
#Override
public void onError(FacebookException exception) {
}
});
Succeeded! sorry for your time and thanks.
AccessToken accessToken = AccessToken.getCurrentAccessToken();
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
// object will contain the user data
}
});

Quickblox login facebook Android

I search many on stackoverflow but don't get answer yet.
loginButtonFb.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
QBUsers.signInUsingSocialProvider(QBProvider.FACEBOOK, facebookAccessToken, null, new QBEntityCallbackImpl<QBUser>() {
#Override
public void onSuccess(final QBUser user, Bundle args) {
ChatService.getInstance().login(user, new QBEntityCallbackImpl() {......................}
and it gets error:
Token is required
Can anyone give me some method to do this?
Add this before signIn:
facebookAccessToken = loginResult.getAccessToken();
This way you sign in using the facebook session. Hope it helps !
enter code here
AccessToken token = AccessToken.getCurrentAccessToken();
String fb_token=token.getToken();
After logging in through facebook Successfully put the following code to get the current access token of the user

Android - Parse.com NullPointerException when saving object

In my app I am using Parse.com and Facebook login, and the Facebook login is working and I am now trying to save the email of the user who signed. I am doing this like this:
Request.newMeRequest(ParseFacebookUtils.getSession(), new Request.GraphUserCallback() {
// callback after Graph API response with user object
#Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String email = user.getProperty("email").toString();
Log.i(TAG, email);
ParseUser currentUser = ParseUser.getCurrentUser();
currentUser.put("email", email);
currentUser.saveInBackground();
}
}
}).executeAsync()
But then when I run I get java.lang.NullPointerException at this line currentUser.put("email", email); But I log email before this and it is not null and I have a email attribute in my User class on parse, so Im not sure why this?
Here the code that comes before to set up the FB Login you should need it and it is working but just in case.
public void onLoginClick(View v) {
progressDialog = ProgressDialog.show(LoginActivity.this, "", "Logging in...", true);
final List<String> permissions = Arrays.asList("public_profile", "email");
// NOTE: for extended permissions, like "user_about_me", your app must be reviewed by the Facebook team
// (https://developers.facebook.com/docs/facebook-login/permissions/)
ParseFacebookUtils.logIn(permissions, this, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException err) {
progressDialog.dismiss();
if (user == null) {
Log.d(TAG, "Uh oh. The user cancelled the Facebook login.");
} else if (user.isNew()) {
Log.d(TAG, "User signed up and logged in through Facebook!");
showSelectSchoolActivity();
} else {
Log.d(TAG, "User logged in through Facebook!");
showMainActivity();
}
}
});
Thanks for the help :)
for checking the response you should always use (err==null) as the condition for success. Only after you have recieved the response from the server and err==null then you can check for current user. You should also have a check for when err!=null. Then get the error message from the err.getCode() and check what the error code is.

Categories

Resources