Firebase,Android和Google Plus登录 - 无法检索用户的电子邮件地址 [英] Firebase, Android, Google Plus Login - Not Able To Retrieve User's Email Address

查看:425
本文介绍了Firebase,Android和Google Plus登录 - 无法检索用户的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩Google Plus身份验证和Firebase。



目前我正在通过Android的基本身份验证示例。基本身份验证部分与github上的代码一起工作正常,并且它可以正常工作。 https://github.com/firebase/firebase-login-demo-android

现在问题出现在我试图获取电子邮件地址时,出于某种原因,它似乎不存在于authData对象中。

关于Google身份验证的Firebase文档
$ b


getProviderData()。get(email) -
Google用户主要电子邮件地址列在他们的个人资料。
仅当有效的电子邮件地址可用时才返回,并且用户授予Google 电子邮件权限


如果我在activity_main.xml中添加一个textview
.....

.....



然后在我的代码MainActivity.java中:

pre $ private private void setAuthenticatedUser(AuthData authData) {
if(authData!= null){
/ *隐藏所有登录按钮* /
......
mLoggedInStatusTextView.setVisibility(View.VISIBLE);
mEmailTextView =(TextView)findViewById(R.id.email_textView);
mEmailTextView.setText(authData.getProviderData()。get(email)。toString());
mEmailTextView.setVisibility(View.VISIBLE);
......
}

我得到一个错误:
尝试在空对象引用上调用虚方法'java.lang.String java.lang.Object.toString()'

此外,在Android Studion with调试,当我看着authData对象,电子邮件没有设置,但其他值是 - auth,提供者,提供者数据,令牌,uid,到期等...

<除了获取电子邮件地址,一切似乎都很好。我不确定是否需要任何附加权限。在我的AndroidManifest.xml文件中,我有以下内容:

$ p $ < uses-permission android:name =android.permission.INTERNET />
< uses-permission android:name =android.permission.USE_CREDENTIALS/>
< uses-permission android:name =android.permission.GET_ACCOUNTS/>

从我读到的,GET_ACCOUNTS权限应该足够了。关于为什么Firebase似乎无法获取它的任何想法?编辑:此外,我可以将我的用户数据保存到Firebase user-auth.html#section-storingrel =nofollow> https://www.firebase.com/docs/android/guide/user-auth.html#section-storing

  if(authData.getProviderData()。containsKey(email)){
map.put(displayName,authData.getProviderData ()获得( 电子邮件)的toString());





$ b

然后这个电子邮件值总是空的,所以它不会从Google取回电子邮件再加上。

解决方案

这周我遇到了这个问题。即使通过添加所需的范围,我仍然无法访问用户的电子邮件。 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);
    }

这篇关于Firebase,Android和Google Plus登录 - 无法检索用户的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆