How to get your own API credentials

Here we describe how to obtain two credentials that are important when working with a Google API:

This can be important for both users and developers:

Get a Google Cloud Platform project

You will need a Google Cloud Platform (GCP) project to hold your credentials.

Go to the Google Developers Console:

This console is your general destination for inspecting and modifying your GCP projects.

Create a new project here, if necessary. Otherwise, select the project of interest, if you have more than one.

Enable API(s)

Enable the relevant APIs(s) for your GCP project.

In the left sidebar, navigate to APIs & Services > Library.

Identify the API of interest. Click Enable.

API Key

Some APIs accept requests to read public resources, in which case the request can be sent with an API key in lieu of a token. If this is possible, it’s a good idea to expose this, because then it’s possible to user the wrapper package in a “de-authed” mode. When using the package in a non-interactive or indirect fashion (e.g. a scheduled job on a remote server or via Shiny), it is wonderful to NOT have to manage a token, if the work can be done with an API key instead.

Some APIs aren’t really usable without a token, in which case an API key may not be relevant and you can ignore this section.

Package maintainers might want to build an API key in as a fallback, possibly taking some measures to obfuscate the key and limit its use to your package.

Package users could register an API key for use with a wrapper package. For example, in googledrive, one would use googledrive::drive_auth_config() to store a key for use in downstream requests.

OAuth client ID and secret

Most APIs are used to create and modify resources on behalf of the user and these requests must include the user’s token. A regular user will generally need to send an OAuth2 token, which is obtained under the auspices of an OAuth “app” or “client”.

The basic steps are described in the Prerequisites section for doing Google OAuth 2.0 for Mobile & Desktop Apps:

Two ways to package this info for use with httr or gargle, both of which require an object of class httr::oauth_app:

  1. Use httr::oauth_app().
    • The client ID goes in the key argument.
    • The client secret goes in the secret argument.
  2. Use gargle::oauth_app_from_json().
    • Provide the path to the downloaded JSON file.

In both cases, I suggest you devise a nickname for each OAuth credential and use it as the credential’s name in Google Developer Console and as the appname argument to httr::oauth_app() or gargle::oauth_app_from_json().

Package maintainers might want to build this app in as a fallback, possibly taking some measures to obfuscate the client ID and secret and limit its use to your package.

Package users could register this app for use with a wrapper package. For example, in googledrive, one would use googledrive::drive_auth_config() to store an app for use in downstream requests.

Further reading

Learn more in Google’s documentation: