Generate Access Key For Service Account

Generate Access Key For Service Account Average ratng: 6,8/10 6687 reviews

For local workstations, in order to interact with the API, you’ll want to create an access key and secret key under your IAM user. If you’re working with multiple AWS accounts and you have an IAM user in each account, you’ll need separate access/secret key pairs for each. To create access keys for your AWS account root user, you must use the AWS Management Console. A newly created access key has the status of active, which means that you can use the access key for CLI and API calls.

Service accounts are special Google accounts that can be used by applications to access Google APIs programmatically via OAuth 2.0. A service account uses an OAuth 2.0 flow that does not require human authorization. Instead, it uses a key file that only your application can access. This guide discusses how to access the Content API for Shopping with service accounts.

Note: Applications using service accounts for authentication can only access your own Merchant Center account. If you are writing a third-party application that needs access to your clients' Merchant Center accounts, please see the Authorizing Requests guide instead.

Prerequisites

  • A Merchant Center account.

Steps to using a service account to access the Content API for Shopping

Generate Access Key For Service Account
  1. Generate service-account credentials or access the public credentials you've already generated. You will need to create an OAuth 2.0 Client ID and obtain a *.json private key file:
    1. Go to the Google API Console.
    2. Select a project in the drop-down menu at the top of the page. If you do not have one yet, create one by clicking Create Project.
    3. If you have not already enabled the Content API for Shopping for this project, then search for it in the list of Google APIs and enable it.
    4. In the sidebar on the left, select Credentials. You may need to click the left-pointing arrow at the top left to see this.
    5. To set up a service account, select Create credentials, and then Service account key.
    6. On the next page, select New service account from the drop-down list.
    7. Name the new service account. This also serves as the default username for the service account ID. Remember the service account ID, including the part after the '@' character, for use later.

      The choice of role for the service account will not have any effect on what calls can be made to the Content API, as access to Content API methods is determined instead by the role associated with the service account ID in Merchant Center. If you are unsure what to pick, just pick Project►Viewer.

    8. Select JSON for the key type, then click Create.
    9. The Create button will change to Creating.., and once the key generation finishes, it will automatically download the private key as a *.json file.

      Important: Protect the *.json key file that allows a service account to access the Google services for which it has been authorized. It is good practice to allow service accounts to only access one Google API each. This is a preventative measure to mitigate the amount of data an attacker can access in the situation that the service account’s *.json key file is compromised.

    10. You will be returned to the Credentials page, and you should see the new service account in the list of service account keys for your account.
  2. Add the new service account as a user to your Merchant Center account. If you are a third party developer, you will need to have your client do this step for you.
    1. Go to your Merchant Center account.
    2. Go to the 'Users' list in the settings of your Merchant Center account.
    3. Click the + button, and use the service account ID as the email address for the new user.

      If you did not take note of the service account ID earlier, go to the Service Accounts administration page and select the project you created.

    4. Specify User access and Email notifications, note that use of the Accounts service requires Admin access.
    5. Click on the blue box to the left of Cancel. You will be returned to the list of users, and the service account ID should be listed with the chosen user role(s).
    6. Repeat the process for all other service accounts you want to add.
    7. View existing service account users by going to the Users tab. These will be users with an email address ending in 'gserviceaccount.com'.
  3. Now you can access your Merchant Center account using the service account either by using the Google Application Default Credentials flow or by using the service account flow directly. The Content API for Shopping Samples show how to use both flows for service account credentials in each supported programming language. Please check out the code samples to try out your new service account and to learn what changes you will need to use service accounts in your own code.

Frequently asked questions

Can I log into the Merchant Center web user interface with my service account?

No, service accounts are not regular Google accounts and cannot access the Merchant Center web user interface.

How often do I need to refresh service account access tokens?

Access tokens expire one hour after they are issued by the Google OAuth 2.0 Authorization Server. When an access token expires, the application should use the client library to fetch another access token.

Important: If you are working with Google Cloud Platform, unless you plan to build your own client library, use service accounts and a Cloud Client Library instead of performing authorization explicitly as described in this document. For more information, see Authentication Overview in the Google Cloud Platform documentation.

The Google OAuth 2.0 system supports server-to-server interactions such as those between a web 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. This scenario is sometimes called 'two-legged OAuth,' or '2LO.' (The related term 'three-legged OAuth' refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)

Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses Google Cloud Datastore for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API.

G Suite domain administrators can also grant service accounts domain-wide authority to access user data on behalf of users in the domain.

This document describes how an application can complete the server-to-server OAuth 2.0 flow by using either a Google APIs client library (recommended) or HTTP.

With some Google APIs, you can make authorized API calls using a signed JWT instead of using OAuth 2.0, which can save you a network request. See Addendum: Service account authorization without OAuth.

Overview

To support server-to-server interactions, first create a service account for your project in the API Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account.

Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server.

Finally, your application can use the access token to call Google APIs.

Recommendation: Your application can complete these tasks either by using the Google APIs client library for your language, or by directly interacting with the OAuth 2.0 system using HTTP. However, the mechanics of server-to-server authentication interactions require applications to create and cryptographically sign JSON Web Tokens (JWTs), and it's easy to make serious errors that can have a severe impact on the security of your application.

For this reason, we strongly encourage you to use libraries, such as the Google APIs client libraries, that abstract the cryptography away from your application code.

Creating a service account

A service account's credentials include a generated email address that is unique and at least one public/private key pair. If domain-wide delegation is enabled, then a client ID is also part of the service account's credentials.

If your application runs on Google App Engine, a service account is set up automatically when you create your project.

If your application runs on Google Compute Engine, a service account is also set up automatically when you create your project, but you must specify the scopes that your application needs access to when you create a Google Compute Engine instance. For more information, see Preparing an instance to use service accounts.

If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google API Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following:

  1. Open the Service accounts page.
  2. If prompted, select a project, or create a new one.
  3. Click Create service account.
  4. In the Create service account window, type a name for the service account, and select Furnish a new private key. If you want to grant G Suite domain-wide authority to the service account, also select Enable G Suite Domain-wide Delegation.
  5. Click Create.

Your new public/private keypair is generated and downloaded to your machine; it serves as theonly copy of this key. You are responsible for storing it securely. If you lose this keypair,you will need to generate a new one.

You can return to the API Console at any time to view the email address, public key fingerprints, and other information, or to generate additional public/private key pairs. For more details about service account credentials in the API Console, see Service accounts in the API Console help file.

Take note of the service account's email address and store the service account's P12 private key file in a location accessible to your application. Your application needs them to make authorized API calls.

Note: You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys.

Delegating domain-wide authority to the service account

If you have a G Suite domain—if you use G Suite, for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the Google Calendar API to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as 'delegating domain-wide authority' to a service account.

Note: When you use G Suite Marketplace to install an application for your domain, the required permissions are automatically granted to the application during installation. You do not need to manually authorize the service accounts that the application uses. The account must have domain-wide delegation before the application is installed.Note: Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of G Suite end users to share documents outside of the domain would not apply to service accounts.

To delegate domain-wide authority to a service account, first enable domain-wide delegation for an existing service account in the Service accounts page or create a new service account with domain-wide delegation enabled.

Then, an administrator of the G Suite domain must complete the following steps:

  1. Go to your G Suite domain's Admin console.
  2. Select Security from the list of controls. If you don't see Security listed, select More controls from the gray bar at the bottom of the page, then select Security from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
  3. Select Show more and then Advanced settings from the list of options.
  4. Select Manage API client access in the Authentication section.
  5. In the Client Name field enter the service account's Client ID. You can find your service account's client ID in the Service accounts page.
  6. In the One or More API Scopes field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
  7. Click Authorize.

Your application now has the authority to make API calls as users in your domain (to 'impersonate' users). When you prepare to make authorized API calls, you specify the user to impersonate.

Preparing to make an authorized API call

Java

After you obtain the client email address and private key from the API Console, use the Google APIs Client Library for Java to create a GoogleCredential object from the service account's credentials and the scopes your application needs access to. For example:

If you are developing an app on Google Cloud Platform, you can use the application default credentials instead, which can simplify the process. Key challenges faced by your generation.

Delegate domain-wide authority

If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account with the setServiceAccountUser method of the GoogleCredential factory. For example:

Use the GoogleCredential object to call Google APIs in your application.

Python

After you obtain the client email address and private key from the API Console, use the Google APIs Client Library for Python to complete the following steps:

  1. Create a Credentials object from the service account's credentials and the scopes your application needs access to. For example:

    If you are developing an app on Google Cloud Platform, you can use the application default credentials instead, which can simplify the process.

  2. Delegate domain-wide authority

    If you have delegated domain-wide access to the service account and you want to impersonate a user account, use the with_subject method of an existing ServiceAccountCredentials object. For example:

Use the Credentials object to call Google APIs in your application.

HTTP/REST

Recommendation: Although your application can complete these tasks by directly interacting with the OAuth 2.0 system using HTTP, the mechanics of server-to-server authentication interactions require applications to create and cryptographically sign JSON Web Tokens (JWTs), and it's easy to make serious errors that can have a severe impact on the security of your application.

For this reason, we strongly encourage you to use libraries, such as the Google APIs client libraries, that abstract the cryptography away from your application code.

After you obtain the client ID and private key from the API Console, your application needs to complete the following steps:

  1. Create a JSON Web Token (JWT, pronounced, 'jot') which includes a header, a claim set, and a signature.
  2. Request an access token from the Google OAuth 2.0 Authorization Server.
  3. Handle the JSON response that the Authorization Server returns.

The sections that follow describe how to complete these steps.

If the response includes an access token, you can use the access token to call a Google API. (If the response does not include an access token, your JWT and token request might not be properly formed, or the service account might not have permission to access the requested scopes.)

When the access token expires, your application generates another JWT, signs it, and requests another access token.

The rest of this section describes the specifics of creating a JWT, signing the JWT, forming the access token request, and handling the response.

Creating a JWT

A JWT is composed of three parts: a header, a claim set, and a signature. The header and claim set are JSON objects. These JSON objects are serialized to UTF-8 bytes, then encoded using the Base64url encoding. This encoding provides resilience against encoding changes due to repeated encoding operations. The header, claim set, and signature are concatenated together with a period (.) character.

A JWT is composed as follows:

The base string for the signature is as follows:

Forming the JWT header

The header consists of two fields that indicate the signing algorithm and the format of the assertion. Both fields are mandatory, and each field has only one value. As additional algorithms and formats are introduced, this header will change accordingly.

Service accounts rely on the RSA SHA-256 algorithm and the JWT token format. As a result, the JSON representation of the header is as follows:

The Base64url representation of this is as follows:

Forming the JWT claim set

The JWT claim set contains information about the JWT, including the permissions being requested (scopes), the target of the token, the issuer, the time the token was issued, and the lifetime of the token. Most of the fields are mandatory. Like the JWT header, the JWT claim set is a JSON object and is used in the calculation of the signature.

Required claims

The required claims in the JWT claim set are shown below. They may appear in any order in the claim set.

NameDescription
issThe email address of the service account.
scopeA space-delimited list of the permissions that the application requests.
audA descriptor of the intended target of the assertion. When making an access token request this value is always https://oauth2.googleapis.com/token.
expThe expiration time of the assertion, specified as seconds since 00:00:00 UTC, January 1, 1970. This value has a maximum of 1 hour after the issued time.
iatThe time the assertion was issued, specified as seconds since 00:00:00 UTC, January 1, 1970.

The JSON representation of the required fields in a JWT claim set is shown below:

Additional claims

In some enterprise cases, an application can request permission to act on behalf of a particular user in an organization. Permission to perform this type of impersonation must be granted before an application can impersonate a user, and is usually handled by a domain administrator. For more information on domain administration, see Managing API client access.

To obtain an access token that grants an application delegated access to a resource, include the email address of the user in the JWT claim set as the value of the sub field.

NameDescription
subThe email address of the user for which the application is requesting delegated access.

If an application does not have permission to impersonate a user, the response to an access token request that includes the sub field will be an error.

An example of a JWT claim set that includes the sub field is shown below:

Encoding the JWT claim set

Like the JWT header, the JWT claim set should be serialized to UTF-8 and Base64url-safe encoded. Below is an example of a JSON representation of a JWT Claim set:

Computing the signature

JSON Web Signature (JWS) is the specification that guides the mechanics of generating the signature for the JWT. The input for the signature is the byte array of the following content:

The signing algorithm in the JWT header must be used when computing the signature. The only signing algorithm supported by the Google OAuth 2.0 Authorization Server is RSA using SHA-256 hashing algorithm. This is expressed as RS256 in the alg field in the JWT header.

Sign the UTF-8 representation of the input using SHA256withRSA (also known as RSASSA-PKCS1-V1_5-SIGN with the SHA-256 hash function) with the private key obtained from the Google API Console. The output will be a byte array.

The signature must then be Base64url encoded. The header, claim set, and signature are concatenated together with a period (.) character. The result is the JWT. It should be the following (line breaks added for clarity):

Below is an example of a JWT before Base64url encoding:

Below is an example of a JWT that has been signed and is ready for transmission:

Making the access token request

After generating the signed JWT, an application can use it to request an access token. This access token request is an HTTPS POST request, and the body is URL encoded. The URL is shown below:

The following parameters are required in the HTTPS POST request:

NameDescription
grant_typeUse the following string, URL-encoded as necessary: urn:ietf:params:oauth:grant-type:jwt-bearer
assertionThe JWT, including signature.

Below is a raw dump of the HTTPS POST request used in an access token request:

Below is the same request, using curl:

Handling the response

If the JWT and access token request are properly formed and the service account has permission to perform the operation, then the JSON response from the Authorization Server includes an access token. The following is an example response:

Access tokens can be reused until during the duration noted in the expires_in value.

Calling Google APIs

Java

Use the GoogleCredential object to call Google APIs by completing the following steps:

  1. Create a service object for the API that you want to call using the GoogleCredential object. For example:
  2. Make requests to the API service using the interface provided by the service object. For example, to list the instances of Cloud SQL databases in the exciting-example-123 project:

Python

Use the authorized Credentials object to call Google APIs by completing the following steps:

  1. Build a service object for the API that you want to call. You build a a service object by calling the build function with the name and version of the API and the authorized Credentials object. For example, to call version 1beta3 of the Cloud SQL Administration API:
  2. Make requests to the API service using the interface provided by the service object. For example, to list the instances of Cloud SQL databases in the exciting-example-123 project:

HTTP/REST

After your application obtains an access token, you can use the token to make calls to a Google API on behalf of a given service account or user account if the scope(s) of access required by the API have been granted. To do this, include the access token in a request to the API by including either an access_token query parameter or an Authorization HTTP header Bearer value. When possible, the HTTP header is preferable, because query strings tend to be visible in server logs. In most cases you can use a client library to set up your calls to Google APIs (for example, when calling the Drive Files API).

You can try out all the Google APIs and view their scopes at the OAuth 2.0 Playground.

HTTP GET examples

A call to the drive.files endpoint (the Drive Files API) using the Authorization: Bearer HTTP header might look like the following. Note that you need to specify your own access token:

Here is a call to the same API for the authenticated user using the access_token query string parameter:

curl examples

You can test these commands with the curl command-line application. Here's an example that uses the HTTP header option (preferred):

Or, alternatively, the query string parameter option:

When access tokens expire

Access tokens issued by the Google OAuth 2.0 Authorization Server after the duration provided by the expires_in value. When an access token expires, then the application should generate another JWT, sign it, and request another access token.

Addendum: Service account authorization without OAuth

With some Google APIs, you can make authorized API calls using a signed JWT directly as a bearer token, rather than an OAuth 2.0 access token. When this is possible, you can avoid having to make a network request to Google's authorization server before making an API call.

If the API you want to call has a service definition published in the Google APIs GitHub repository, you can make authorized API calls using a JWT instead of an access token. To do so:

  1. Create a service account as described above. Be sure to keep the JSON file you get when you create the account.
  2. Get the API's service name and API name from the service definition file on GitHub. For example, the Firestore API service definition file, firestore.yaml, has the following contents:
  3. Using any standard JWT library, such as one found at jwt.io, create a JWT with a header and payload like the following example:
    • For the kid field in the header, specify your service account's private key ID. You can find this value in the private_key_id field of your service account JSON file.
    • For the iss and sub fields, specify your service account's email address. You can find this value in the client_email field of your service account JSON file.
    • For the aud field, specify https://SERVICE_NAME/, using the values from the service definition file.
    • For the iat field, specify the current Unix time, and for the exp field, specify the time exactly 3600 seconds later, when the JWT will expire.

Sign the JWT with RSA-256 using the private key found in your service account JSON file.

For example:

Java

Using google-api-java-client and java-jwt:

Python

Generate Access Key For Service Account Email

Using PyJWT:

Generate Access Key For Service Account Manager

  1. Call the API, using the signed JWT as the bearer token: