How to access Google APIs and services using Android

I already did a post on how to access Google APIs and services using various language. It is now time for a quick tutorial on accessing Google APIs and services using Android. More precisely, it will look at the configuration that needs to be done on Google APIs Console in order to grant access to your Android application.

Disclaimer: I have not yet published an application using this, so it is possible that something is missing. I will update this post as soon as I have more information (or remove this disclaimer if nothing more is needed).

Basic setup

In my other post, I already went over the basic setup. You will need to follow two of the sections in the first post: Enable needed APIs and Setup OAuth Consent Screen. This will configure the basis of your Google project.


Gather project information

Before even going to the Google APIs Console, you will need to find two information from your Android Application:

  1. Your package name (can be found in the AndroidManifest.xml file of your application);
  2. The SHA1 fingerprint from your application

Assuming that you are using Gradle, you can run the android/signingReport task in your app context. The output of this task will be shown in your Gradle console. Looking at the output from that task, you’ll be able to find the SHA1 signature associated with your application.


Create credentials

With the previous information in hand, you can now go in the Google APIs console to allow your application to connect. In order to do so, you’ll need to go into the Credentials section, hit Create credentials and select OAuth Client ID.

The next page is the one where you will need all that information you gathered. The first thing you’ll need to do is select the type of application: Android. With this done, you will be able to select the remaining options:

  1. Name
    This is the name of your application, it is used only in the Google APIs console;
  2. Signing-certificate fingerprint
    This is the SHA1 signature you extracted from your project in the previous step;
  3. Package name
    This is the package name you extracted in the previous step;

Creating a new credential for Android

Creating a new credential for Android

Specifying the android credential information

Specifying the android credential information


All done! Your Android application should now be able to connect to the services.


Sources

How to access Google APIs and services

For a side project of mine, I needed to access Google APIs, more precisely, I needed Google Tasks API access. This tutorial will show how to access google APIs and services.

The easiest way to access the various Google APIs is to use one of their language-specific wrappers. These will require you to provide a client secrets file. This file will contain all the information to access the services you enabled.  All the backend configuration is done through Google APIs Console.

Warning: It’s not quite clear if this file is safe to be shared with anyone (aka, checked into a public repository, distributed to clients, etc.). The way I personally see it is that this file allows clients to connect to the Google services you enable. Some of these services have a quota, others are simply paid. Therefore, anyone getting your file can easily go over your quota or get you billed quite a bit in a month. Basically, what I am saying is that I would not distribute this file to other people.

Enable needed APIs

After creating your project, the first thing you’ll need to do is select which APIs you want to use. In order to do so, simply go into the Dashboard section and hit the Enable APIs and Services button. The next page will allow you to search and select which APIs you want to enable. Once an API is selected, a page describing it and allowing you to enable the API will be shown. Check this page carefully, some APIs have quotas or are paid services.


Getting to the enable APIs page

Getting to the enable APIs page

Searching for APIs to enable

Searching for APIs to enable

Enabling an API

Enabling an API


Setup OAuth Consent Screen

The client secrets file is only one part of the OAuth process. It will be used to access your configuration, quotas, make you pay for services, etc. It is provided by the software/tool/website maker. The second part of the process is the client information. This will be provided by the user of the software/tool/website and will allow the maker to access some information about the user.

For example, a website could request access to the Google Drive content of a user. The APIs quotas that will be used are the ones from the owner of the website, but the information accessed will be the one of the user.

Before the website is allowed to access information about the client, the client must consent to share this information. Google requires you to customize this consent screen before it can be used. In order to do so, you will need to go to the Credentials section, then select the OAuth Consent Screen tab. In there, you will be able to customize quite a few information to make your consent screen nice and professional. The bare minimum is to select an email address and a product name. Both of these will be shown to the user when asking him to consent to the sharing his information.


Customizing the consent screen

Customizing the consent screen


Create credentials and download client secrets file

All the grunt work is now done for your project. The last piece of the puzzle is to create the credential and get the file. In order to create the credentials, you will need to go to the Credentials section and hit the Create Credentials button. In the submenu, you will need to select OAuth client ID. The next page will allow you to select the type of application. According to the Google documentation, there should be an Installed application option in there. That option would have been perfect for me (working on a command line tool here). In the end, I went with the only thing that made sense: Other.

Once your credential is created, simply go to the Credentials section, find the credential you want to use and hit the download icon. All done!


Creating a new credential

Creating a new credential

Specifying the credential information

Specifying the credential information

Downloading client secrets file

Downloading client secrets file


Sources