Hello Analytics API: Java quickstart for service accounts

This tutorial walks through the steps required to access a Google Analytics
account, query the Analytics APIs, handle the API responses, and output the
results. The Core Reporting API v3.0,
Management API v3.0, and OAuth2.0
are used in this tutorial.

Create a client ID

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.
Then click Save.

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.

When prompted for the Key type select JSON, and save the
generated key as
client_secrets.json; you will need it later in the tutorial.

Add service account to Google Analytics account

The newly created service account will have an email address,
&ltprojectId&gt-&ltuniqueId&gt@developer.gserviceaccount.com;
Use this email address to
add
a user to the Google analytics account you want to access via the API.
For this tutorial only
Read
& Analyze permissions are needed.

Step 2: Install the Google Client Library

To install the Google Analytics API Java Client, you must download a zip file
containing all of the jars you need to extract and copy into your Java
classpath.