Google uses a centralised way to manage their APIs interactions. In this quick tip we’re going to see how we can set up a new project, activate APIs, manage credentials, etc. Before getting started you need to have a Google account.
##Registering a New Project
The Google Developers Console is our main dashboard. The left sidebar is our main menu, so make sure you navigate to the projects page and click the
create project button.
You only need to type your project name, and you’ll have an auto generated id that you can change. After clicking the submit button, the process may take a few seconds to complete and you’ll be redirected after that to your project dashboard.
Before creating your OAuth credentials you will need to configure your consent screen. This widget is shown to the user when granting permissions to your application, you only need to select an email address and enter a product name.
Navigate to the credentials page from the left menu. This page is split into two sections:
OAuth: This is where you can create your
client_secret. Select your application type (in this case Web application) and make sure your authorized origins and redirect URIs are correct.
Developer Key: This key is used by Google to track your application consumption. We will create a new
Browser Key, fill the input with your request origin and click
Now you have your credentials ready to use, if you are using the official
google/apiclient PHP package, your authentication code will look like the following:
$this->client = new Google_Client; $this->client->setClientId('Your Client ID'); $this->client->setClientSecret('Your Client Secret'); $this->client->setDeveloperKey('Your API Key'); $this->client->setRedirectUri("http://host/loginCallback"); $this->client->setScopes();// we will talk about this in a moment
##Activating Desired APIs
We activate the APIs depending on our application needs, lets start working with Google Analytics API as an example, a sample code look like the following.
$service = new Google_Service_Analytics($this->client);// our configured client $accounts = $service->management_accounts->listManagementAccounts();
This will throw an error, because our project doesn’t have access to the API yet. Navigate to the
APIs page from the left menu of your Google Developers Console. You have the ability to switch the API ON and OFF. After activating the API, you can select it to see the details of your application consumption.
The next step is to specify the list of scopes needed by your application, this part depends on the selected API and you must check the documentation for the available options.
In this quick tip we explained the common process of creating a new project on Google Developers Console. The remaining part depend on your application domain and can’t be covered with details.