Authenticate Google API’s

Authenticate google api’s

In my last article, you can learn about tracking your react application using GA, In this article, you can learn how to authenticate google API’s. I hope by the end of this article you will get a good knowledge of Google API authentication.

Assumes you already have a Google account, For doing authentication, you have to redirect Google Developer Console.

Developer console

Then, click CREATE PROJECT, you need to give the project name and organization name(if have). Then click create button after that it will redirect to the dashboard page.

Create project

Now we are going to focus on authentication, Click the credentials option in the side nav, it will redirect.

Authentication methods

There is a three type of authentication methods..,
* API keys
* OAuth 2
* Service to service authentication

The API key is less secure and restricted in scope and usage by Google.

OAuth 2 is meant to let your app make requests on behalf of a user, and as such, the process is more complicated than needed and requires exposing URLs to handle callbacks. Way too complex for simple uses.

In a Service to Service authentication method, the application directly talks to the Google API, using a service account, by using a JSON Web Token. In this article, we can see the Service to Service authentication.

Service to Service Authentication:

In this method, first, we need to generate a key file through the developer console(credentials).

There are two ways to generate a key file..,
* JSON key file
* P12 key file

JSON key file is a more simple and most used way than P12. If we use the P12 key file means, we need to convert that into a .pem file, then we can use it. It’s no longer recommended by Google, so just use JSON.

For creating service to service authentication, you have to click Create Credentials button at the top, then select the service account authentication. Then you have to redirect the service account creation page.

service account authentication

Give the service account name and description, then click create, after that you have an option to service account access and user account access it is optional, If want you can assign the roles or else directly click Done.
After the creation of service account authentication, you can see this page.

service account

For generating a key file, you have to click the key option in the nav, then click Create New Key option, then it will show the modal to select the key file type, choose JSON and click create. Then, the key will download to your computer

create new key
JSON key file

You can directly add the JSON key file in your application folder, and call it in code, or else you can store that JSON file data’s as an environment variable and use it in our code.
Sample JSON:

{
"type": "service_account",
"project_id": "XXXXXXX",
"private_key_id": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX80",
"private_key": "-----BEGIN PRIVATE KEY-----\XXXXXXXXXXXXX=\n-----END PRIVATE KEY-----\n",
"client_email": "XXXXX@tXXXXXXX.iam.gserviceaccount.com",
"client_id": "XXXXXXXXXXXXXXXXXX",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "XXXX",
"client_x509_cert_url": "XXXXXXX"
}

Now, we can see the auth integration sample code only, we will see the full integration using nodejs in my another article.

const jwt = new google.auth.JWT(key.client_email, null, key.private_key, scopes)

Here, client_email and private_key is from JSON file, we can see about the scopes now.,
Google Analytics API defines several scopes:

  • https://www.googleapis.com/auth/analytics.readonly- view the data
  • https://www.googleapis.com/auth/analytics - view and manage the data
  • https://www.googleapis.com/auth/analytics.edit - edit the management entities
  • https://www.googleapis.com/auth/analytics.manage.users - manage the account users and permissions
  • https://www.googleapis.com/auth/analytics.manage.users.readonly - view the users and their permissions
  • https://www.googleapis.com/auth/analytics.provision - create new Google Analytics accounts.

This is the way to enable service to service account authentication.

Enabling API’s in the developer console:
Then you have to enable whatever API you want, now am going to enable Google Analytics Reporting API and Google Analytics API. In the search bar of the developer console, you have to search the API and click enable button for that selected API.

Enabled API

This is the simplest and easiest way to authenticate google API’s. I hope this article will help you to authenticate Google API.

Happy learning!!!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Necessity of Failure

Integrate AppGallery Connect’s Cloud DB Service in Three Easy Steps

Build simple API service with Python FastAPI — Part 1

Flutter Firestore Tutorial Part 2/2

How to set up mutt (text-based mail client) with Gmail?

Importance of Data Centers

Automation of Technologies: A step towards working smartly

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Krishnaveni

Krishnaveni

More from Medium

Project: Habit Tracking App

Deny Access With URL Rewrite Based On Custom Header

HOW TO SETUP WORDPRESS PHP IN KUBERNATES WITH MYSQL DATABASE{MYSQL-DB}

CVE-2022-0847: Dirty Pipe Vulnerability Exploitation in Android