Google Python API: The easy way

When life gives you APIs, just automate it. 🙂

As a developer or tech geek, when technology is part of your lifestyle or work, we definitely look forward to exploring all developer things. APIs & libraries are one of the important things we generally look for.

Why is it so? Because, when we use that specific technology on a daily basis, we definitely want to automate most of the things. For that, we try to explore its functional part just to make our work easy. We can use that functional part [ API/Library ], to make an automation script or application.

According to Google or we can say Wikipedia, Google APIs are application programming interfaces developed by Google, that allow communication with Google Services and their integration with other services.

Being a tech giant, Google provided APIs or libraries supporting multiple programming languages, especially python. Google has its own documentation for each Google resource that we can follow and automate or use Google resources using a programming language.

Base Structure

The base structure of any Google API resource can be the same but there are slightly different objects which we need to take care of.

There can be a more detailed structure that may or may not include multiple objects but when we create a Google API script for any Google resource, there are few things that can be common for all resources and while creating an automation script or application, these are the things that we will always create inside the script:

  • Google APIs Libraries
  • Extra dependent libraries
  • Credential object
  • Build service object
  • Collection object
script structure

We will discuss each thing we mentioned above and we will see how can we commonly use specific objects inside the script.

Prerequisites

Auth user

When we call any Google resource API using python or any programming language, we need Authenticated user which helps us to create a connection between the application/script and the Google resource of a specific user.

We can create Auth user using the google cloud console and there are multiple options that we can use to create Authenticated user. The options provided by Google are:

  • OAuth 2.0 Client IDs
  • Service Accounts

For now, we are using Service Accounts. The main reason for using Google Cloud Service Account is because Service Accounts provide all functionality and options for non-human interaction & Google resource API official documentation is also used by service accounts for API calls. According to official documentation, one of the options mentioned that when we required non-human access. We can use the service account.

Google Cloud Console link — “https://console.cloud.google.com

Enable APIs

Even having a service account, we cannot access any of the Google resources, it is because, by default, all the APIs for a specific Google account are disabled due to which user is not able to make a call with Google resources. This is the kind of security provided by Google which specifies that, enable if there is the requirement of API calls of that specific resource. For each Google resource, we need to enable Google resource API separately. You can get the list of apps from Google Resources which provides API calls here.

Library install

There is a prerequisite for google API which includes the python version & dependent libraries.

Create a separate virtual environment for the project

virtualenv <PROJECTNAME>

Creating a separate virtual environment helps us to manage python packages separately. Activate the project by running the below command

source <PROJECTNAME>/bin/activate

Install dependent libraries for google authentication & resources access

pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib

When you will list down all the Google python libraries, it will show more than 4–5 libraries which shows that google API python libraries use other predefined builds which require multiple purposes and that’s the beauty of python [means if you want to implement anything, you can create your own or use the pre-defined library].

pip list

Output:

Package                  Version
------------------------ ---------
cachetools 4.2.4
certifi 2021.10.8
charset-normalizer 2.0.9
google-api-core 2.3.2
google-api-python-client 2.33.0
google-auth 2.3.3
google-auth-httplib2 0.1.0
google-auth-oauthlib 0.4.6
googleapis-common-protos 1.54.0
httplib2 0.20.2
idna 3.3
oauthlib 3.1.1
pip 21.3.1
protobuf 3.19.1
pyasn1 0.4.8
pyasn1-modules 0.2.8
pyparsing 3.0.6
requests 2.26.0
requests-oauthlib 1.3.0
rsa 4.8
setuptools 58.3.0
six 1.16.0
uritemplate 4.1.1
urllib3 1.26.7
wheel 0.37.0

Full code: https://github.com/b44rawat/Python

Authentication

Create credential object

To create a credential/accessibility object, we will use the service_account module which we will import from google.oauth2.

While creating we need to take care of two things:

  • Credential location: Credential location refers to JSON file location
  • Scope: Scope refers to the reach of a specific credential object, which means it defines what kind of permission we are allowing to a specific set of credentials. To know more about scoping, visit the official documentation provided by Google.
from google.oauth2 import service_accountSCOPE=['']
CREDENTIAL='/path/to/credentials.json'creds = service_account.Credentials.from_service_account_file(CREDENTIAL, scopes=SCOPE)

Check below example

from googleapiclient.discovery import build
from google.oauth2 import service_accountSCOPE=[‘openid’, ‘https://www.googleapis.com/auth/userinfo.email', ‘https://www.googleapis.com/auth/userinfo.profile']
CREDENTIAL='credentials.json'creds = service_account.Credentials.from_service_account_file(CREDENTIAL,scopes=SCOPE)

Resource Access

Create service object

To create a service object, we will use the build function which we import from the googleapiclient.discovery package.

Build function helps users to create a service object which defines what kind of Google resources they are accessing.

There can be multiple Args we can define in

from googleapiclient.discovery import build
user_info_service = build('<GOOGLE-RESOURCE>', '<API-VERSION>', credentials=<CREDENTIAL-OBJECT>)

Let’s break down the above build function, there can be more Args that we can provide but for now, we are defining only three.

  • <GOOGLE-RESOURCE> defines the name of the resource Google that we want to use. It can be any Google resources like Docs, Sheets, Youtube, Drive, etc. Get the list of google resources from the Google API Python Github.
  • <API-VERSION> defines the version provided by Google for specific Google resources, get more information from the official documentation.
  • credentials=<CREDENTIAL-OBJECT> defines keyword argument which refers to the credentials object we define above.
  • user_info_service variable holds the service information which we provide.

Collection object

Once we create a service object which provides and create access to specific Google resource. We will use service objects to fetch resource information. Each API service provides access to one or more resources. A set of resources of the same type is called a collection.

user_info = user_info_service.<INNER-FUNCTIONS()>.execute()

OAuth example:

user_info = user_info_service.userinfo().get().execute()
  • user_info_service is a service object which holds the information of the google resources like the name of the resource, API version & credentials object location.
  • userinfo() is an instance or resource method that we will use to get information of credentials object
  • get() is another inner method that comes under userinfo() method which defines the specific purpose of fetching user information.
  • execute() use to execute the actual information provided by us. Without this, we will only get memory objects which we cannot compile. So, for that, we need to execute which will provide actual value.

Below is the combined code of all the above things we mentioned. The below code gives the email id associated with the following credentials.

Full code

Below is the full and actual representation of the above terms which we discuss.

from googleapiclient.discovery import buildfrom google.oauth2 import service_accountSCOPE=['openid', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile']CREDENTIAL='credentials.json'creds = service_account.Credentials.from_service_account_file(CREDENTIAL,scopes=SCOPE)auth_info = build('oauth2', 'v2', credentials=creds)user_info = auth_info.userinfo().get().execute()print(user_info['email'])

To know more about google resources API:

https://github.com/b44rawat/Python/tree/main/googleapi/authentication

Multi resource

We discussed a single google resource in which we create a single credential object and that single credential object is used to create a service object and further we use a service object to execute the task.

In some scenarios, we want to use multiple resources within the specific script. For that, we can re-use the same credential object & Google APIs libraries which will help us to use the same memory object to achieve things.

The only thing we need to take care of is that if there are multiple Google accounts required in a single application or script, we cannot use the same credential object for that. We need to create different credential objects for different Google service accounts.

Conclusion

There are lots of Google resources APIs & libraries which provide partially or fully access to Google resources.

There are common steps for all the Google resources that we can perform and achieve some behavior depending on the Google service. This thing can include Credential object, build service object & Collection object.

There is the documentation for each Google resource provided by Google. It can be a bit confusing but when we deep dive into it, we can get more about the allocation and modules use-cases for each Google resource.

Image reference

Links:


Blog Pundit: Sandeep Rawat

Opstree is an End to End DevOps solution provider

Connect Us

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: