I am doing simple facebook login using latest facebook sdk 4.8 but after login i am getting null value no user details are coming but only i am getting the details if the user is developer and administrator of that Facebook App.
And also i have given facebook permission.
loginButton.setReadPermissions(Arrays.asList("public_profile", "user_likes",
"user_friends", "email", "user_birthday", "user_photos", "user_about_me"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {
try{
nameTxt.setVisibility(View.VISIBLE);
nameTxt.setText(jsonObject.getString("name"));
}catch(Exception e){
e.printStackTrace();
Log.d("FacebookException", e.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,gender,birthday,email,bio,photos{link}");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
Have you switched your app status to live? You can see status in your app's Dashboard in developers console.
If not, just switch status to live in Status & Review tab. This switch should be switched to 'Yes' position
Related
I am not able to get the gender here in OnCompleted function
I am able to get other params like id,name,email
Currently app is in Dev mode
This was working yesterday.
Code:
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
try {
AccessToken token = AccessToken.getCurrentAccessToken();
String accessToken = token.getToken().toString();
String expiresAt = token.getExpires().toString();
String id = object.getString("id");
String gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
System.out.println(e.toString());
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id, name, email, gender, location, picture.type(large)");
request.setParameters(parameters);
request.executeAsync();
In your https://developers.facebook.com/ account, under App Review section, click Start a Submission, select user_gender from LOGIN PERMISSIONS and go through submission steps.
After facebook will approve user_gender permission, you can use it.
Again in your developer account click Settings then Advanced and in Upgrade API Version
change the API version your app calls to v3.1 and using below code, you will get the gender of the user.
private void attemptToLogIn() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("email", "user_gender"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
makeGraphRequest(loginResult.getAccessToken());
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
}
public void makeGraphRequest(AccessToken token) {
GraphRequest request = GraphRequest.newMeRequest(
token, (object, response) -> {
try {
String gender = object.getString("gender");
} catch (JSONException e) {
e.printStackTrace();
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "name,gender");
request.setParameters(parameters);
request.executeAsync();
}
Your access token need the user_gender permission:
https://developers.facebook.com/docs/facebook-login/permissions/v3.0#reference-user_gender
I am logging into facebook on Android using facebook android sdk (4.7.0). Most of the time it shows following dialog and works fine.
But sometimes it shows the following dialog and login does not work.
What are possibilites for failed login in this case? I am testing it on android (5.1.1).
facebookButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday"));
callbackManager = CallbackManager.Factory.create();
facebookButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
fbid = object.getString("id");
fbfirst_name = object.getString("first_name");
fblast_name = object.getString("last_name");
fbemail = object.getString("email");
new FBRegistrationService().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender,birthday,first_name,last_name");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
}
});
Update: I am also getting these logs.
"Blink deferred a task in order to make scrolling smoother. Your timer and network tasks should take less than 50ms to run to avoid this. Please see https://developers.google.com/web/tools/chrome-devtools/profile/evaluate-performance/rail and https://crbug.com/574343#c40 for more information.", source: https://m.facebook.com/login.php?skip_api_login=1&api_key=350893648453711&signed_next=1&next=https%3A%2F%2Fm.facebook.com%2Fv2
I have been using the Facebook SDK for Android a bit now. I have been having trouble where one of my testers could not create an account in my app using Facebook. After some debugging I figured out that for some reason when he tries to login / register the JSON object that contains the information does not come with his email. This only happens for HIS account!
Does anyone know why this could happen?
(I have included a simplified version of my login code so you can see if there is anything wrong with it that might cause it)
List<String> permissionNeeds = Arrays.asList("public_profile", "user_friends", "email");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(final LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
Log.d(TAG, object.toString()); // Has no email sometimes!
} catch (JSONException e) {
e.printStackTrace();
Log.e(TAG, String.format("Error with facebook login: %s", e.getMessage()));
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,picture,email,gender");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, String.format("Error with facebook login: %s", error.getMessage()));
}
});
loginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(SplashActivity.this,
Arrays.asList("public_profile", "user_friends", "email"));
}
});
If any more information is needed, please tell me I'll be happy to add some.
Thanks!
So turns out that my tester had an inactive email account.
In general you should never be counting on every Facebook user having an email address.
I am using customize Facebook login button .this is my code. I'm using session manager to store email and name for entire app.I have used logout button in third activity .
How to logout from Facebook
public void FB_Login(View view){
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// AccessToken.getCurrentAccessToken().getPermissions();
// Profile profile = Profile.getCurrentProfile();
// String firstName = profile.getFirstName();
// System.out.println(profile.getProfilePictureUri(20,20));
// System.out.println(profile.getLinkUri());
// App code
AccessToken.getCurrentAccessToken().getPermissions();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
email = object.getString("email");
name=object.getString("name");
// Creating user login session
// For testing i am stroing name, email as follow
// Use user real data
session.createLoginSession(name, email);
// Staring MainActivity
if (session.isLoggedIn()) {
in = new Intent(HomePageActivity.this, HomeSearchActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
finish();
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.v("LoginActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Log.v("LoginActivity", "cancel");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.v("LoginActivity", exception.getCause().toString());
}
});
}
Anyone help me. please.
You can logout from Facebook using Facebook Graph API.
LoginManager.getInstance().logOut();
I hope it helps!
You need to use :
LoginManager.getInstance().logOut();
At First, You need to Initialize SDK:
FacebookSdk.sdkInitialize(getApplicationContext());
Then
LoginManager.getInstance().logOut();
Courtesy
I have successfully installed the android sdk v4.0 in my studio.
I get to login correctly using LoginButton.
So far everything ok.
I want to have a string for each user data collecting interests me, for example:
String name = USER_NAME
String = USER_MAIL mail.
With the 3.0 sdk had it properly.
With the new SDK can not I do this, I followed the instructions in this thread:
Android Facebook 4.0 SDK How to get Email, Date of Birth and gender of User
I read in the documentation and in various threads about "Using the Graph API" but I can not get the data I'm looking for and assign them to each string.
this is my code (just as it is now):
LoginButton loginButton;
private CallbackManager mCallbackManager;
//...
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday, user_friends"));
mCallbackManager = CallbackManager.Factory.create();
//...
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
Toast.makeText(Ready.this, "Connected!", Toast.LENGTH_SHORT).show();
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// App code
Log.v("LoginActivity", "cancel");
}
#Override
public void onError(FacebookException exception) {
// App code
Log.v("LoginActivity", exception.getCause().toString());
}
});
As I said before, I connect with facebook perfectly, but do not know how to assign, for example, email to a string and name to another string.
Thank you very much in advance.
If you meant how to get name and email from the GraphRequest, it's simply this:
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
try {
String id=object.getString("id");
String name=object.getString("name");
String email=object.getString("email");
String gender=object.getString("gender");
String birthday=object.getString("birthday");
//do something with the data here
} catch (JSONException e) {
e.printStackTrace(); //something's seriously wrong here
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
GraphResponse contains things like the HTTPConnect used, any error encountered, etc. Assuming everything goes as expected, only JSONObject object is needed to retrieve the data requested.
My working code for getting the profile information
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
profile = Profile.getCurrentProfile();
if(profile != null){
fbUserId = profile.getId();
editor.putString("UserName",profile.getFirstName()+" "+profile.getLastName());
editor.putString("FbId",profile.getId());
editor.putString("ProfilePicture",profile.getProfilePictureUri(20,20).toString());
editor.commit();
}
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
And you can track the profile changes here:
profileTracker = new ProfileTracker() {
#Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile currentProfile) {
if(currentProfile != null){
fbUserId = currentProfile.getId();
if(!sharedPreferences.contains("UserName")){
editor.putString("UserName",currentProfile.getFirstName()+" "+currentProfile.getLastName());
}
if(!sharedPreferences.contains("FbId")){
editor.putString("FbId",currentProfile.getId());
}
if(!sharedPreferences.contains("ProfilePicture")){
editor.putString("ProfilePicture",currentProfile.getProfilePictureUri(100,100).toString());
}
editor.commit();
}
}
};