1

I followed the official document for implementing sing-in with Google. It was not difficult, but the official document stopped at getting the basic information such as name or e-mail. I need to get birthday.

So, when creating the option, I added this:

.requestScopes(Scope("https://www.googleapis.com/auth/user.birthday.read"))

Since the official document stopped there I could not get how to get birthday. So I have searched the web and found a method using a plain HTTP GET request to https://people.googleapis.com/v1/people/me. (Or is this covered by the Google Play Services API?)

But the method needs the access token, which was not included in the GoogleSignInAccount which was returned after signing in. There was an example for getting the access ID by creating an OAuth server ID and then use getServerAuthCode() to get the access token, but that is complicated and it is not done on a server, it is done inside the app, right after singing in.

In this case, do I have to create an OAuth server ID, and use the access token to get the birthday?

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135

1 Answers1

2

After a lot of Google searching, I have found this example. Before finding this, I found an example of using Plus.API, but this answer said it was deprecated. The funny thing is, now even this answer is outdated and does not work. I had to search a lot more to fix it. If only Google's sign-in document page included an example of getting additional data other than the default fields, all these miseries could have been avoided.

Anyways, this is the code that worked for me.

First, sign in with Google according to the official documentation.
Then,

val HTTP_TRANSPORT = AndroidHttp.newCompatibleTransport();
val JSON_FACTORY = JacksonFactory.getDefaultInstance();

inner class DamnTask(var email:String) :AsyncTask<Unit,Unit,Unit>()
{
    override fun doInBackground(vararg params: Unit?)
    {
        var credential = GoogleAccountCredential.usingOAuth2(context,
                Collections.singletonList(Scopes.PROFILE));
        credential.selectedAccount = Account(email, "com.google");

        var service = People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("MyApp")
                .build();

        var me = service.people().get("people/me")
                .setRequestMaskIncludeField("person.Birthdays")
                .execute();
        Log.d("damn", "Birthday " +me.birthdays.toString());
    }
}

To make it work, I had to add the following libraries to the dependencies.

compile 'com.google.api-client:google-api-client:1.23.0'
compile 'com.google.api-client:google-api-client-android:1.22.0'
compile 'com.google.apis:google-api-services-people:v1-rev4-1.22.0'

Also, I had to enable the "People" API in the project's settings. This needs to be done in the Google's web console. I forgot the URL, but if you do not enable it, the exception message will show the exact URL to enable it.

You can find more field names here.

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135