Set up the Geocoding API

European Economic Area (EEA) developers

This document describes the steps needed to start using the Geocoding API.

Google Maps Platform products are secured from unauthorized use by requiring API calls to include either an API key or OAuth token.

Create an OAuth token

Geocoding API supports the use of OAuth 2.0 for authentication. Google supports common OAuth 2.0 scenarios such as those for a web server.

This document describes how to pass an OAuth token to the Geocoding API call in your development environment. For instructions on using OAuth in a production environment, see Authentication methods at Google.

About OAuth

There are many ways to create and manage access tokens with OAuth based on your deployment environment.

For example, the Google OAuth 2.0 system supports server-to-server interactions, such as those between your application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. For more information on authentication methods, see Authentication methods at Google.

Alternatively, you might use the Geocoding API as part of an Android or iOS mobile app. For general information on using OAuth with the Geocoding API, including information on managing access tokens for different deployment environments, see Using OAuth 2.0 to Access Google APIs.

About OAuth scopes

To use OAuth with the Geocoding API, the OAuth token must be assigned the correct scope. Geocoding API supports the following scopes:

  • https://www.googleapis.com/auth/maps-platform.geocode — Use with all Geocoding API endpoints.
  • https://www.googleapis.com/auth/maps-platform.geocode.address — Use only with GeocodeAddress for forward geocoding.
  • https://www.googleapis.com/auth/maps-platform.geocode.location — Use only with GeocodeLocation for reverse geocoding.
  • https://www.googleapis.com/auth/maps-platform.geocode.place — Use only with GeocodePlace for place geocoding.

Also, you can use the general https://www.googleapis.com/auth/cloud-platform scope for all Geocoding API endpoints. That scope is useful during development because it is the default scope used when creating tokens using gcloud.

Example: Try REST API calls in your local development environment

If you want to try the Geocoding API using an OAuth token, but don't have an environment setup to generate tokens, you can use the procedure in this section to make the call.

This example describes how to use the OAuth token provided by Application Default Credentials (ADC) to make the call. For information about using ADC to call Google APIs using client libraries, see Authenticate using client libraries.

Prerequisites

Before you can make a REST request using ADC, use the Google Cloud CLI to provide credentials to ADC:

  1. Install and initialize the gcloud CLI.
  2. Run the following gcloud command on your local machine to create your credential file:

    gcloud auth application-default login
  3. A login screen is displayed. After you log in, your credentials are stored in the local credential file used by ADC.

For more information, see Set up ADC for a local development environment.

Make a REST request

In this example, you pass two request headers:

  • Pass the OAuth token in the Authorization header by using the following command to generate the token:

    gcloud auth application-default print-access-token

    The returned token has a scope of https://www.googleapis.com/auth/cloud-platform.

  • Pass the ID or name of your Google Cloud project that has billing enabled in the X-Goog-User-Project header.

The following example makes a call to the Geocoding API using an OAuth token.

  1. Edit the PROJECT_ID field to pass the ID of your Google Cloud project.

    curl -X GET -H 'Content-Type: application/json' \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "X-Goog-User-Project: PROJECT_ID" \
    "https://geocode.googleapis.com/v4beta/geocode/address/1600+Amphitheatre+Parkway,+Mountain+View,+CA"
    
  2. Click the copy icon in the code sample, Select the copy icon., to copy the curl command.
  3. Paste the command in a terminal window and run the command.

The response is a JSON object:

Troubleshooting

If your request returns an error message about end-user credentials not being supported by this API, see Troubleshoot your ADC setup.