3

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.

Ishan
  • 3,303
  • 5
  • 29
  • 47

2 Answers2

2

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.

Ishan
  • 3,303
  • 5
  • 29
  • 47
  • Thanks, works like charm. I'd missed on providing the `.addScope(Plus.SCOPE_PLUS_LOGIN)` and `.addScope(Plus.SCOPE_PLUS_PROFILE)` – zIronManBox Aug 16 '15 at 07:59
0

On your onConnected() method, you can get it with:

Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String accountName = Plus.AccountApi.getAccountName(mGoogleApiClient);
Anyonymous2324
  • 250
  • 3
  • 12
  • I was able to get AccountName successfully but not DisplayName. I am getting java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String com.google.android.gms.plus.model.people.Person.getDisplayName()' on a null object reference – Ishan Apr 12 '15 at 17:36
  • Here are the changes that I made https://github.com/ishanatmuz/GoogleFitTest/commit/967df7bd214fe2f0d8125fd44c2fb877adb5a582 – Ishan Apr 12 '15 at 17:42
  • `Plus.PeopleApi.getCurrentPerson()` is probably returning null because you don't have the permission to see the details for the user. When building the `GoogleApiClient`, make sure to add the right scope. See here: https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.Builder.html – Anyonymous2324 Apr 12 '15 at 17:55
  • I am sorry I meant to accept your answer as correct before you asking. I think the error is because I haven't added appropriate Scopes. so I tried this. .addScope(Plus.SCOPE_PLUS_LOGIN) .addScope(Plus.SCOPE_PLUS_PROFILE) But this is still giving me the null pointer reference error. – Ishan Apr 12 '15 at 17:56
  • Try `.addScope(Plus.SCOPE_PLUS_PROFILE)` instead. – Anyonymous2324 Apr 12 '15 at 17:58
  • I am referring from here https://developer.android.com/reference/com/google/android/gms/plus/People.html#getCurrentPerson(com.google.android.gms.common.api.GoogleApiClient) I have added both the Scopes Plus.SCOPE_PLUS_LOGIN and Plus.SCOPE_PLUS_PROFILE It is still giving me the same null pointer reference error. Here are the changes https://github.com/ishanatmuz/GoogleFitTest/commit/8ce5aedd5fa8614653d98016f99e745b90f51f95 – Ishan Apr 12 '15 at 18:04
  • hmm.. Sorry, I'm out of ideas. :( Try posting a new question about getting the user profile and put the code on how you are initializing your Google Api Client in the question. – Anyonymous2324 Apr 12 '15 at 18:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75064/discussion-between-ishan-and-anyonymous2324). – Ishan Apr 12 '15 at 18:09