I am trying to get the user email and birthday when user uses facebook login for my app.
This is how I am trying to do it:
public class MainActivity extends AppCompatActivity {
private LoginButton facebookLoginButton;
private CallbackManager callbackManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getApplication());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
facebookLoginButton = (LoginButton)findViewById(R.id.fb_login_button);
facebookLoginButton.setReadPermissions(Arrays.asList("email","public_profile", "user_birthday"));
facebookLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
String email = me.optString("email");
String name = me.optString("name");
String birthday = me.optString("user_birthday");
System.out.println(email);
System.out.println(name);
System.out.println(birthday);
}
}
}).executeAsync();
String userID = loginResult.getAccessToken().getUserId();
String AuthToken = loginResult.getAccessToken().getToken();
System.out.println(userID);
System.out.println(AuthToken);
}
Only name, userID and AuthToken is printed successfully, I am not getting values for birthday and email (I use email for my facebook account and provided birthday so there should be value).
Am i using the permission wrong?
Replace your GraphRequest code with This
GraphRequest mGraphRequest = GraphRequest.newMeRequest(
loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject me, GraphResponse response) {
if (response.getError() != null) {
// handle error
} else {
String email = me.optString("email");
String name = me.optString("name");
String birthday = me.optString("user_birthday");
System.out.println(email);
System.out.println(name);
System.out.println(birthday);
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,gender, birthday");
mGraphRequest.setParameters(parameters);
mGraphRequest.executeAsync();
String userID = loginResult.getAccessToken().getUserId();
String AuthToken = loginResult.getAccessToken().getToken();
System.out.println(userID);
System.out.println(AuthToken);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.