I am using this code for fetching fb user info for my android app.
My issue is that this code doesn't returns Date of Birth of all users i.e, If I login through through user A, it returns DOB but if I login through user B it doesn't. Both the users have saved dob in fb and also have privacy settings.
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList(
"public_profile", "email", "user_birthday", "user_friends"));
callbackManager = CallbackManager.Factory.create();
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("LoginActivity", response.toString());
// Application code
String email = object.getString("email");
String birthday = object.getString("birthday"); // 01/31/1980 format
}
});
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());
}
});
}
If anybody knows something please share.
I assume that not both users have a role in the App and user_birthday is not approved. Please take a look at the docs about Login Review: https://developers.facebook.com/docs/facebook-login/review
Related
How to implement this login with facebook?
I've followed this tutorial https://www.androidlearning.in/facebook-login-for-android-app/ but it throws a complete activity (the traditional way).
I have facebook application installed
In applications like Memrise, Bandlab, etc. Shows me that Dialog but no in my application
Also I try with
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList([...]);
but dont works...
I want:
My app Show me:
I was facing the same problem as you 30 minutes ago, but I was using a custom button for Facebook and the following code inside onCreate for calling Facebook Login:
loginButton = (CircularProgressButton) findViewById(R.id.btn_fb);
loginButton.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "user_friends"));
}
});
//Register a callback
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
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 {
nome = object.getString("name");
email = object.getString("email");
String idfb = loginResult.getAccessToken().getUserId();
logarFb(idfb, nome, email);
} catch(JSONException ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
//cancelled
}
#Override
public void onError(FacebookException exception) {
//error
}
});
I changed it to Facebook Login button:
<com.facebook.login.widget.LoginButton
android:id="#+id/btn_fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
Now that's how my code looks like now, much cleaner:
private LoginButton loginButton;
private CallbackManager callbackManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
callbackManager = CallbackManager.Factory.create();
loginButton = findViewById(R.id.btn_fb);
loginButton.setReadPermissions("email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
getUserDetails(loginResult);
}
#Override
public void onCancel() {
funcoes.aviso(MainActivity.this,"Você cancelou o login",R.color.red500,3000, R.drawable.ic_triste);
}
#Override
public void onError(FacebookException exception) {
funcoes.dialogoMsg(MainActivity.this,"Há algo de errado com o login do Facebook :/");
}
});
}
protected void getUserDetails(final LoginResult loginResult) {
GraphRequest data_request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted( JSONObject object, GraphResponse response) {
try {
nome = object.getString("name");
email = object.getString("email");
String idfb = loginResult.getAccessToken().getUserId();
logarFb(idfb, nome, email);
} catch(JSONException ex) {
ex.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
data_request.setParameters(parameters);
data_request.executeAsync();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
If do you have Facebook App installed, that's should be enough to have the Facebook Login as Dialog Box instead of an FullScreen Activity.
If you want so, follow this guide If you forgot some other setting: https://www.studytutorial.in/android-facebook-integration-and-login-tutorial
The dialog will appear only if you have the facebook app installed on your phone , other wise you will get a web view. Try installing the app and then logging in.
You need to remove the permission "user_birthday" If you defined in your permissions.
LoginManager.getInstance().logInWithReadPermissions(context,Arrays.asList("public_profile","email"));
As some permissions needs for "App Review"
I have changed my permission set
Existing code
LoginManager.getInstance().logInWithReadPermissions(activity, listOf("user_photos", "email", "public_profile"))
Working code
LoginManager.getInstance().logInWithReadPermissions(activity, listOf("email"))
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
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 want to use Facebook SDK to get my photo's url, and display it. I log in with fb sdk, but each time I open my app, I must log out and log in again to have access token. Can I have acces token without log in fb? And can I have access token of my friend's page? Please help me.
This is my code:
public class MainActivity extends Activity {
private CallbackManager callbackManager;
private ImageView image;
private LoginButton loginButton;
private AccessToken accessToken;
JSONObject jsObject = null;
public MyAsyncTask myAsy;
public String url;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList("user_photos"));
loginButton.registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
accessToken = loginResult.getAccessToken();
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object,
GraphResponse response) {
// Get images's url code
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "photos{images}");
request.setParameters(parameters);
request.executeAsync();
}
#Override
public void onCancel() {
// info.setText("Login attempt cancelled.");
}
#Override
public void onError(FacebookException e) {
// info.setText("Login attempt failed.");
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
1) No, you can't get a user access token without first logging the user in. Access tokens have an expiry time and have to be renewed/re-issued from time to time.
2) If by "can I get an access token for a friend's page" mean if you can get an access token for a friend without said friend doing anything, then no, you can't. You need to have that friend login to your app just like every other user.
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();
}
}
};