且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Firebase,Android和Google Plus登录 - 无法检索用户的电子邮件地址

更新时间:2023-12-01 15:57:04

这周我遇到了这个问题。即使通过添加所需的范围,我仍然无法访问用户的电子邮件。 authData回调总是有相同的字段,无论我添加的范围。

我试过 .addScope(new Scope(email) ) .addScope(新范围(https://www.googleapis.com/auth/plus.profile.emails.read))但是什么都没有实现。

我设法检索用户电子邮件的唯一方法是使用Google的 onConnected 方法($)
$ b

  if(Plus.PeopleApi.getCurrentPerson(mGoogleApiClient)!= null){
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
Log.i(email,email);
}


I am playing around with Google Plus Authentication and Firebase.

I am working through the basic auth example with Android at the moment. The basic authentication part is working fine with the code on github, and it works as it should. https://github.com/firebase/firebase-login-demo-android

The problem now arises when I am trying to fetch the email address, for some reason it doesn't seem to exist in the authData object.

From the Firebase documentation on Google authentication:

getProviderData().get("email") - The Google user's primary email address as listed on their profile. Returned only if a valid email address is available, and the Google email permission was granted by the user.

If I add a textview in 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 get an error: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference

Additionally, in Android Studion with Debug on, when I look into the authData object, email is not set, but other values are - auth, provider, providerData, token, uid, expires, etc...

Everything seems to be working great except fetching the email address. I'm not sure if any addiontial 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 SHOULD be enough. Any ideas on why Firebase can't seem to fetch it?

EDIT: Additionally, I am able to 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 empty, so it's not retrieving the email from Google Plus at all.

I've run into this problem this week. Even by adding the needed scopes, I was still unable to access to the user's email. The authData callback always had the same fields no matter what scope I added.

I've tried .addScope(new Scope("email")) and .addScope(new Scope("https://www.googleapis.com/auth/plus.profile.emails.read")) but achieved nothing.

The only way I managed to retrieve the user's email was by using the Google's onConnected method and asking by the user's email there.

if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
        Log.i("email", email);
    }