Would you like to connect to a Google API using a service account with PHP? You have come to the right place, in this tutorial we are going to go though the steps needed to connect to the Google Analytics API with a service account.
Accessing the Google APIs using a service account is quite useful. Some times you just want to access your own data and not data owned by other users. In this instance there is no reason to use OAuth2 and prompt a user to give you access to there information, its your information you already have access. This is why we use a service account. This tutorial uses the Google Analytics API to walk you though the steps of creating a service account and how to then access your GA data with it.
What is a Service Account.
A service account will allow your application to access your Google Analytics data without prompting a user for access. A service account is essentially Oauth1. Oauth2 is when a user is prompted to give your application permission to use it. If you are going to only be access your own data then you should be using a Service Account. If you want to access data owned by another user then you should be using Oauth2 and looking at a different tutorial. You can find it here Google Oauth2 PHP
PHP client library
In this tutorial we will be using the newest version of the Google Client library for PHP it can be found on Github. The google-api-php-client client library is updated regularly whenever anything changes. You will need to copy the entire src/Google directory to the directory of your application. I don’t recommend only taking the files that you need unless you really know what you are doing.
Google Developers Console
Before you can access any of the Google APIs you need to first create your application in the Google Developers console. I have a separate tutorial that goes though the steps of creating an application on the Google Developers console you can find it here. Google Cloud Console Once you have completed the basic set up including selecting which API that you want to access. For this tutorial we will be using Google Analytics. Make sure you have filed in both an Email Address and a product name under the consent screen or it wont work.
Creating Service Account
On the Credentials screen you will see a button that says Create new clientid, click on it. In the popup window selected the “Service account” radio button. A file will begin to download you will need this file later. Copy the Email Address that was created for you it should look something like this: email@example.com
A service account doesn’t need to prompt a user for access because you have to set it up. Go to the Google Analytics website in the Admin section for the Account you want to retrieve data from. This is very important it must be at the account level add this email address as a new user. just give them read access.
Using a service account to access your Google Analytics data can be every useful if you would like to store some of your data on your system for further processing. By using a service account you remove the need to prompt a user to give your application access, as long as the application email has access in the Google Analytics account this script will with no need for user intervention. You can see this in action at Google Analytics Reporting API