Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

onConnected is called when the user has succesfully signed in and provided the permissions. 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.

Any help will be greatly appreciated.

like image 859
Ishan Avatar asked Dec 28 '25 02:12

Ishan


1 Answers

With the help of @Anyonymous2324 I figured out the solution. 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

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).
  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.
  3. In your GoogleApiClient.Builder add the Plus API like this .addApi(Plus.API)
  4. We also need to add some scopes so that getDisplayName can work. These are .addScope(Plus.SCOPE_PLUS_LOGIN) and .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. Therefore it is better to check the currentPerson object before calling getDisplayName on it. 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; adding permission for INTERNET seems like a good idea. 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.

But don't take my word on it and test yourself.

like image 127
Ishan Avatar answered Dec 30 '25 14:12

Ishan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!