Firebase, Android, Google Plus Login - Cannot Get User Email Address
I am playing with Google Plus Authentication and Firebase.
I am working on a basic auth example from Android at the moment. The basic authentication part works fine with the code on github and it works as it should. https://github.com/firebase/firebase-login-demo-android
The problem comes up when I try to get the email, for some reason it doesn't exist in the authData object.
From Firebase Documentation on Google Authentication :
getProviderData (). get ("email") - The primary Google email address listed on their profile. Refundable only if a valid email address is available and Google permission has
been granted by the user.
If i add textview to activity_main.xml ..... .....
Then in my code MainActivity.java:
private void setAuthenticatedUser(AuthData authData) {
if (authData != null) {
/* Hide all the login buttons */
......
mLoggedInStatusTextView.setVisibility(View.VISIBLE);
mEmailTextView = (TextView) findViewById(R.id.email_textView);
mEmailTextView.setText(authData.getProviderData().get("email").toString());
mEmailTextView.setVisibility(View.VISIBLE);
......
}
I am getting the error: Attempting to call the virtual method "java.lang.String java.lang.Object.toString ()" on a null object reference
Also, in Android Studion with Debug on, when I view the authData object, no email is set, but other values: auth, provider, providerData, token, uid, expires, etc.
Everything seems to work fine except for getting the email address. I'm not sure if additional permissions are needed. In my AndroidManifest.xml file I have the following:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
From what I've read, the GET_ACCOUNTS permission MUST be sufficient. Any ideas on why Firebase can't seem to get it?
EDIT: Also, I can save my user data to Firebase https://www.firebase.com/docs/android/guide/user-auth.html#section-storing
if(authData.getProviderData().containsKey("email")) {
map.put("displayName", authData.getProviderData().get("email").toString());
}
Then this email value is always blank, so it doesn't receive an email from Google Plus at all.
source to share
It looks like you need to request an additional area that includes email. If you add this area, you will be able to access your email with getProviderData()
.
Why
Google+ Android login asks for a set of default scopes. By default, this set does not include an email address.
how
Areas are defined at initialization GoogleApiClient
, as described here . Specify the scope of the email.
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope("email") // <-- This requests the email address
.build();
The docs also contain a list of commonly used Google+ sign-in areas .
source to share
I ran into this problem this week. Even adding the required scopes, I still could not access the user's email. The authData callback always had the same fields no matter which scope I added.
I tried .addScope(new Scope("email"))
and .addScope(new Scope("https://www.googleapis.com/auth/plus.profile.emails.read"))
but didn't get anything.
The only way I was able to get the user's email was google method onConnected
and request from the user's email there.
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.i("email", email);
}
source to share
you can use another method other than getProviderData().get("email")
.
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
This should get the user's email address.
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
enough for the permission, so you don't need to add any permissions or scope.
source to share
You can get Firebase User email from Firebase Auth service.
FirebaseAuth.AuthStateListener mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
String email = user.getEmail();
}
}
};
source to share