问题
I'm developing an app which use Firebase` as a BaaS.
I have integrated Google Sign-in and I want to fetch user's name, email address and profile pic.
I have successfully fetched the name and profile pic, but I'm unable to fetch the email address.
Here's Google API client setup:
/* Setup the Google API object to allow Google+ logins */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
Here's what I have done to fetch user's data:
public void fetchDataFromGoogle() {
if (authData != null) {
if (authData.getProvider().equals("google")) {
googleName = (String) authData.getProviderData().get("displayName");
googleEmail = (String) authData.getProviderData().get("email");
googleImage = (String) authData.getProviderData().get("profileImageURL");
}
} else {
Toast.makeText(getBaseContext(), "error", Toast.LENGTH_LONG).show();
}
}
This code have successfully fetched the name and profile pic, but it is returning null
for the email address.
Please let me know what's going wrong here.
回答1:
Not sure if it is your case, but one situation where you are able to get other fields but getEmail returns null is when in Firebase console > Authentication > Sign-In Methods you allow "creation of multiple accounts with the same email address"
There's a closed bug report already filed with google:
When you go to the Auth > Sign-In Methods page of your project in the Firebase console. do you have One account per email address on or off? If you allow multiple accounts per email address, you will get null for FirebaseUser.getEmail()
You have to use the option: "Prevent creation of multiple accounts with the same email address" or else FirebaseUser.getEmail() will return null
Ps: only users who first logged in after the option was turned off will be able to use this method successfully
回答2:
You'll need to also request the email
scope. From the Firebase documentation for Google authentication:
Here is an example of Google login where the session will expire upon browser shutdown and we also request the extended email permission:
var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com");
ref.authWithOAuthPopup("google", function(error, authData) { /* Your Code */ }, {
remember: "sessionOnly",
scope: "email"
});
See also my answer there: Firebase and new Google Sign-In on Android
来源:https://stackoverflow.com/questions/36353638/unable-to-fetch-users-gmail-email-address-through-firebase