简体   繁体   English

如何找到用户用来登录我的基于google-fit的Android应用程序的帐户(电子邮件)?

[英]How to find which account (email) has user used to log into my google-fit based android app?

I am trying to make an android app based on google-fit. 我正在尝试制作一个基于google-fit的Android应用程序。

What I am trying to achieve is that I create an account for the user on my website as soon as the user chooses an account using the app. 我想要实现的是,一旦用户选择使用该应用程序的帐户,我就会在我的网站上为用户创建一个帐户。

Here is the code base that I want to build up upon (It is just the basic registration sample code) https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7 这是我想要构建的代码库(它只是基本的注册示例代码) https://github.com/ishanatmuz/GoogleFitTest/tree/829051b7739ee9d8871c3ba9e5f21dfb17f4f3d7

onConnected is called when the user has succesfully signed in and provided the permissions. 当用户成功登录并提供权限时,将调用onConnected I am calling my own function to do further work which is basically this : 我正在调用我自己的函数来做进一步的工作,基本上是这样的:

  1. Get the information (at least email) of the user who just signed in. 获取刚刚登录的用户的信息(至少是电子邮件)。
  2. Send the user to my server for registration. 将用户发送到我的服务器进行注册。
  3. Continue with the rest of the app. 继续使用应用程序的其余部分。

I need help figuring out how to do the step 1. 我需要帮助搞清楚如何执行第1步。

Any help will be greatly appreciated. 任何帮助将不胜感激。

With the help of @Anyonymous2324 I figured out the solution. 在@ Anyonymous2324的帮助下,我找到了解决方案。 There is little more to do than what's mentioned in the answer below. 没有什么比下面的答案中提到的更多。 So I thought it would be best for anyone who stumbles upon here in future; 所以我认为对将来偶然发现的人来说最好; if I put it all together. 如果我把它们放在一起

To get the accountName (email) or Display Name (user's name), the code required is the same as mentioned by @Anyonymous2324 要获取accountName(电子邮件)或显示名称(用户名),所需的代码与@ Anyonymous2324所述相同

Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);

But to get this to work there are a few things that are needed to be done. 但要实现这一目标,还有一些事情需要完成。

  1. Go to Developer's console and add Google+ API for your project (This is required for the use of any Google+ related work, in our case it is gathering of user name). 转到开发人员的控制台并为您的项目添加Google+ API (这是使用任何与Google+相关的工作所必需的,在我们的情况下,它是收集用户名)。
  2. We need to add permission to access the accounts of the device, by adding <uses-permission android:name="android.permission.GET_ACCOUNTS" /> to the manifest. 我们需要通过在清单中添加<uses-permission android:name="android.permission.GET_ACCOUNTS" />来添加访问设备帐户的<uses-permission android:name="android.permission.GET_ACCOUNTS" />
  3. In your GoogleApiClient.Builder add the Plus API like this .addApi(Plus.API) 在您的GoogleApiClient.Builder添加这样的Plus API .addApi(Plus.API)
  4. We also need to add some scopes so that getDisplayName can work. 我们还需要添加一些范围,以便getDisplayName可以工作。 These are .addScope(Plus.SCOPE_PLUS_LOGIN) and .addScope(Plus.SCOPE_PLUS_PROFILE) 这些是.addScope(Plus.SCOPE_PLUS_LOGIN).addScope(Plus.SCOPE_PLUS_PROFILE)

It is mentioned here that the getCurrentPerson method can return null if the required scopes are not specified or there is a network failure. 这里提到的是,如果未指定所需的范围或网络出现故障,则getCurrentPerson方法可以返回null Therefore it is better to check the currentPerson object before calling getDisplayName on it. 因此,最好在调用currentPerson对象之前调用getDisplayName The complete code then looks like : 完整的代码如下所示:

Person currentPerson = Plus.PeopleApi.getCurrentPerson(mClient);
if(currentPerson != null) {
    String currentPersonName = currentPerson.getDisplayName();
    Log.d(TAG, currentPersonName);
    logStatus(currentPersonName);
}

Since the documentation mentions that the returned value can be null in case of network error; 由于文档提到在网络错误的情况下返回的值可以为null; adding permission for INTERNET seems like a good idea. 添加INTERNET权限似乎是一个好主意。 But on my phone it was working without the internet connection. 但是在我的手机上它没有连接互联网。 I guess it was taking the information from Google+ app on my phone and not going to the internet altogether, so I didn't had to use internet. 我想它是从我的手机上的Google+应用程序中获取信息而不是完全上网,所以我不必使用互联网。

But don't take my word on it and test yourself. 但是不要接受我的话并测试自己。

On your onConnected() method, you can get it with: onConnected()方法上,您可以使用:

Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 与 android 应用 Google-Fit 同步权限 - sync permissions with an android app Google-Fit 如何知道哪个Google帐户(电子邮件ID)用户在我的应用中购买了产品? - How can I know from which Google account (Email id) user purchased a product in my app? 如何获取来自Google-Fit的活动时间 - How to get active minutes from Google-Fit 如何使用Graph API查找Facebook用户上次使用我的应用程序的时间? - How to find When a Facebook User has Last Used my App, using Graph API? 如何向android中的用户电子邮件帐户发送电子邮件 - How to send an email to user email account in android 如何在 Google Play 开发者控制台中找到 Android 应用程序的帐户所有者? - How to find account owner for Android app in Google Play Developer Console? 如何更改play.google中显示的Google开发者帐户电子邮件? - how can I change my google developer account email which shown in play.google? 如何检查这是否是用户第一次使用我的应用程序 - How to check if it is the first time a user has used my app 如何将Android上的Google帐户链接到我的应用程序? - How can I link the Google account on the android to my app? 如何在我的Android应用程序中撤消Google帐户访问权限? - How to revoke google account access within my android app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM