The document describes how an application can gain authorization to make requests to the Management API.
Before users can view their account information on the Google Analytics web site, they must first log in with a Google Account. In the same way, when users first access your application, they need to authorize your application to access their data.
Every request your application sends to the Analytics API must include an authorization token. The token also identifies your application to Google.
About authorization protocols
Authorizing requests with OAuth 2.0
All requests to the Analytics API must be authorized by an authenticated user.
The details of the authorization process, or "flow," for OAuth 2.0 vary somewhat depending on what kind of application you're writing. The following general process applies to all application types:
- When you create your application, you register it using the Google Developers Console. Google then provides information you'll need later, such as a client ID and a client secret.
- Activate the Analytics API in the Google Developers Console. (If the API isn't listed in the Developers Console, then skip this step.)
- When your application needs access to user data, it asks Google for a particular scope of access.
- Google displays a consent screen to the user, asking them to authorize your application to request some of their data.
- If the user approves, then Google gives your application a short-lived access token.
- Your application requests user data, attaching the access token to the request.
- If Google determines that your request and the token are valid, it returns the requested data.
Some flows include additional steps, such as using refresh tokens to acquire new access tokens. For detailed information about flows for various types of applications, see Google's OAuth 2.0 documentation.
Here's the OAuth 2.0 scope information for the Analytics API:
||Read-only access to the Analytics API.|
||Write access to the Analytics API.|
||Edit Google Analytics management entities.|
||View and manage user permissions for Analytics accounts.|
||View Google Analytics user permissions.|
To request access using OAuth 2.0, your application needs the scope information, as well as information that Google supplies when you register your application (such as the client ID and the client secret).
Tip: The Google APIs client libraries can handle some of the authorization process for you. They are available for a variety of programming languages; check the page with libraries and samples for more details.
Common OAuth 2.0 Flows
The following guidelines outline common use cases for specific OAuth 2.0 flows:
This flow is good for automated/offline/scheduled access of a user's Google Analytics data.
- Automatically updating user dashboards with the latest Google Analytics data.
Ideal for when users interact directly with the application to access their Google Analytics data within a browser. This flow eliminates the need for server-side capabilities, but it also makes it impractical for automated/offline/scheduled reporting.
- A browser based reporting tool such as the Google Analytics Query Explorer
For applications that are distributed as a package and installed by the user. It requires that the application or user have access to a browser to complete the authentication flow.
- A desktop widget on a PC or Mac.
- A plugin for a content management system — The benefit of this flow compared to web server or client-side is that a single Developers Console project can be used for your application. This allows for consolidated reporting and a simpler installation for users.
Useful for automated/offline/scheduled access to Google Analytics data for your own account. For example, to build a live dashboard of your own Google Analytics data and share it with other users.
There are a few steps you need to follow to configure service accounts to work with Google Analytics:
To get started using Analytics API, you need to first create or select a project in the Google Developers Console and enable the API. Using this link guides you through the process and activates the Analytics API automatically.
Alternatively, you can activate the Analytics API yourself in the Developers Console by doing the following:
- Go to the Google Developers Console.
- Select a project, or create a new one.
- In the sidebar on the left, expand APIs & auth. Next, click APIs. Select the Enabled APIs link in the API section to see a list of all your enabled APIs. Make sure that the Analytics API is on the list of enabled APIs. If you have not enabled it, select the API from the list of APIs, then select the Enable API button for the API.
- In the sidebar on the left, select Credentials.
In either case, you end up on the Credentials page and can create your project's credentials from here.
To set up a new service account, do the following:
- Under the OAuth heading, select Create new Client ID.
- When prompted, select Service Account and click Create Client ID.
- A dialog box appears. To proceed, click Okay, got it.
Your new Public/Private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely. The Console shows your private key's password only at this initial moment of service account creation--the password will not be shown again. You now have Generate New JSON Key and Generate New P12 Key options, and the ability to delete.
If you are having issues with authentication and are getting
403 status codes here are a couple of steps you can
take to troubleshoot your issues:
You will get a
401 status code if your
has expired or if you are using the wrong scope for the API.
You will get a
403 status code if the authorized user does not
have access to the view (profile). Make sure you are authorized with the
correct user and that they indeed have the view (profile) you have selected.
OAuth 2.0 playground. This is a fantastic tool that allows you to go through the entire authorization flow through a web interface. The tool also displays all the HTTP request headers required for making an authorized query. If you can't get authorization to work in your own application, you should try to get it working through the OAuth 2.0 playground. Then you can compare the HTTP headers and request from the playground to what your application is sending Google Analytics. This check is a simple way to ensure you are formatting your requests properly.
OAuth 1.0 Playground. This is similar to the OAuth 2.0 playground, but for the previous version of OAuth.
If you receive an
invalid_grant error response when attempting to
use a refresh token, the cause of the error may be due to the following reasons:
- Your server's clock is not in sync with NTP.
- The refresh token limit has been exceeded.
Applications can request multiple refresh tokens to access a single Google Analytics account. For example, this is useful in situations where a user wants to install an application on multiple machines and access the same Google Analytics account. In this case, two refresh tokens are required, one for each installation. When the number of refresh tokens exceeds the limit, older tokens become invalid. If the application attempts to use an invalidated refresh token, an
invalid_granterror response is returned. The limit for each unique pair of OAuth 2.0 client and Google Analytics account is 25 refresh tokens (note that this limit is subject to change). If the application continues to request refresh tokens for the same Client/Account pair, once the 26th token is issued, the 1st refresh token that was previously issued will become invalid. The 27th requested refresh token would invalidate the 2nd previously issued token and so on.