如何找到用户使用哪个帐户(电子邮件)登录我的 google-fit 基于 android 的应用程序?
How to find which account (email) has user used to log into my google-fit based android app?
我正在尝试制作一个基于 google-fit 的 android 应用程序。
我想要实现的是,只要用户使用该应用程序选择一个帐户,我就会在我的网站上为用户创建一个帐户。
这是我要构建的代码库(它只是基本的注册示例代码)https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7
onConnected is called when the user has succesfully signed in and provided the permissions. I am calling my own function 做进一步的工作,基本上是这样的:
- 获取刚刚登录的用户的信息(至少是邮箱)。
- 将用户发送到我的服务器进行注册。
- 继续应用程序的其余部分。
我需要帮助来弄清楚如何执行第 1 步。
任何帮助将不胜感激。
在您的 onConnected()
方法中,您可以通过以下方式获取它:
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
在@Anyonymous2324 的帮助下,我找到了解决方案。除了下面的答案中提到的内容之外,没有什么可做的了。所以我认为这对将来偶然发现这里的人来说是最好的;如果我把它们放在一起。
要获取帐户名(电子邮件)或显示名称(用户名),所需的代码与@Anyonymous2324 提到的相同
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
但要让它发挥作用,还需要做一些事情。
- 转到 Developer's console 并为您的项目添加 Google+ API(这需要使用任何 Google+ 相关工作,在我们的例子中是收集用户名)。
- 我们需要通过向清单添加
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
来添加访问设备帐户的权限。
- 在你的
GoogleApiClient.Builder
中添加加号 API 像这样 .addApi(Plus.API)
- 我们还需要添加一些范围,以便
getDisplayName
可以工作。这些是 .addScope(Plus.SCOPE_PLUS_LOGIN)
和 .addScope(Plus.SCOPE_PLUS_PROFILE)
提到here 如果未指定所需范围或出现网络故障,getCurrentPerson
方法可以return null
。因此,最好在调用 getDisplayName
之前检查 currentPerson
对象。
完整的代码看起来像:
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mClient);
if(currentPerson != null) {
String currentPersonName = currentPerson.getDisplayName();
Log.d(TAG, currentPersonName);
logStatus(currentPersonName);
}
由于文档提到 returned 值在网络错误的情况下可以为空;添加互联网权限似乎是个好主意。但是在我的 phone 上它在没有互联网连接的情况下工作。我猜它是从我的 phone 上的 Google+ 应用程序获取信息,而不是完全上网,所以我不必使用互联网。
但不要相信我的话,自己测试一下。
我正在尝试制作一个基于 google-fit 的 android 应用程序。
我想要实现的是,只要用户使用该应用程序选择一个帐户,我就会在我的网站上为用户创建一个帐户。
这是我要构建的代码库(它只是基本的注册示例代码)https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7
onConnected is called when the user has succesfully signed in and provided the permissions. I am calling my own function 做进一步的工作,基本上是这样的:
- 获取刚刚登录的用户的信息(至少是邮箱)。
- 将用户发送到我的服务器进行注册。
- 继续应用程序的其余部分。
我需要帮助来弄清楚如何执行第 1 步。
任何帮助将不胜感激。
在您的 onConnected()
方法中,您可以通过以下方式获取它:
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
在@Anyonymous2324 的帮助下,我找到了解决方案。除了下面的答案中提到的内容之外,没有什么可做的了。所以我认为这对将来偶然发现这里的人来说是最好的;如果我把它们放在一起。
要获取帐户名(电子邮件)或显示名称(用户名),所需的代码与@Anyonymous2324 提到的相同
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
但要让它发挥作用,还需要做一些事情。
- 转到 Developer's console 并为您的项目添加 Google+ API(这需要使用任何 Google+ 相关工作,在我们的例子中是收集用户名)。
- 我们需要通过向清单添加
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
来添加访问设备帐户的权限。 - 在你的
GoogleApiClient.Builder
中添加加号 API 像这样.addApi(Plus.API)
- 我们还需要添加一些范围,以便
getDisplayName
可以工作。这些是.addScope(Plus.SCOPE_PLUS_LOGIN)
和.addScope(Plus.SCOPE_PLUS_PROFILE)
提到here 如果未指定所需范围或出现网络故障,getCurrentPerson
方法可以return null
。因此,最好在调用 getDisplayName
之前检查 currentPerson
对象。
完整的代码看起来像:
Person currentPerson = Plus.PeopleApi.getCurrentPerson(mClient);
if(currentPerson != null) {
String currentPersonName = currentPerson.getDisplayName();
Log.d(TAG, currentPersonName);
logStatus(currentPersonName);
}
由于文档提到 returned 值在网络错误的情况下可以为空;添加互联网权限似乎是个好主意。但是在我的 phone 上它在没有互联网连接的情况下工作。我猜它是从我的 phone 上的 Google+ 应用程序获取信息,而不是完全上网,所以我不必使用互联网。
但不要相信我的话,自己测试一下。