3

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.

4

5 回答 5

1

听起来您需要请求包含电子邮件的附加范围。如果您添加该范围,您将能够使用getProviderData().

为什么

适用于 Android 的 Google+ 登录请求一组默认范围。默认情况下,此集不包括电子邮件。

如何

范围是在初始化时定义的GoogleApiClient,如此处所述。指定电子邮件范围。

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();

文档还包含常用的Google+ 登录范围列表。

于 2015-01-06T23:21:00.683 回答
1

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

我已经尝试过.addScope(new Scope("email")).addScope(new Scope("https://www.googleapis.com/auth/plus.profile.emails.read"))但一无所获。

我设法检索用户电子邮件的唯一方法是使用谷歌的onConnected方法并在那里通过用户的电子邮件进行询问。

if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
        String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
        Log.i("email", email);
    }
于 2015-02-08T19:21:46.257 回答
0

getProviderData().get("email")除了获取用户电子邮件之外,您还可以使用其他方法。

String email = Plus.AccountApi.getAccountName(mGoogleApiClient);

这应该得到用户的电子邮件地址。 <uses-permission android:name="android.permission.GET_ACCOUNTS" />足以获得权限,因此您无需添加任何权限或范围。

于 2015-01-30T08:19:16.560 回答
0

您可以通过 Firebase Auth Service 获取 Firebase 用户电子邮件。

 FirebaseAuth.AuthStateListener mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
              String email = user.getEmail();
            }
        }
    };
于 2016-11-09T12:07:02.647 回答
0

您可以像这样从 Plus 对象获取用户的电子邮件:

if (mGoogleApiClient.isConnected()) {
    String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
}
于 2015-08-31T16:55:30.340 回答