Skip to content

This repository contains the Talend job GetGoogleToken. The goal is to make sure we always have an access token ready to be used to call any Google endpoints in Talend. Can be re used in other jobs.

Notifications You must be signed in to change notification settings

bleyme/get_google_token_talend

Repository files navigation

Getting Google access token with Talend

This repository contains the Talend job GetGoogleToken. The goal is to make sure we always have an access token ready to be used to call any Google endpoints in Talend. Can be re used in other jobs.

Table of contents

Requirements

Features

  • Generate URL with scopes required for your project
  • Getting authorization code generated above to request an access token
  • Refreshing access token with a refresh token
  • Made tokens visible in a file GoogleContexts.csv to lets your other jobs using access tokens easily

Installation

Create Google Cloud project

Create a Google Project at this url: https://console.cloud.google.com/ Then go to APIs & Services > Credentials and create an OAuth Client ID of type "Other": enter image description here

enter image description here

From here Cloud Console will provide you the following :

  • Client ID
  • Client Secret

We will re use those above in our Talend job to get access token in order to made API restful request.

Let's suppose your goal is to have an overview of your Google Drive usage using REST API. To allow Talend requesting Drive, you need to authorize Cloud Console project:

Go to APIs & Services > Library: enter image description here Search for Drive and enable.

Define GoogleContexts.csv location

Here we need to specify in Talend job where will be located your data and GoogleContexts.

Edit Talend job and in context tab, specify the folder location of GoogleContexts.csv in path context: enter image description here

In this example the location of GoogleContexts.csv file is located in the following folder: C:\talend\

Configure GoogleContexts.csv

Once Google cloud project created and API enabled, create a file GoogleContexts.csv (or download it from this repo) in location specified in Define GoogleContexts.csv location.

Fill it with the right credentials:

client_id;YOUR_CLIENT_ID
client_secret;YOUR_SECRET_ID
scope;YOUR_SCOPES

Replace YOUR_CLIENT_ID and YOUR_SECRET_ID with those you created on Create Google Cloud project section.

You need to know the scope required for your project. We still suppose your goal is to do some Google Drive manipulations (with edit rights). In this case your scope will probably be:

https://www.googleapis.com/auth/drive

So here is how it looks like when configured:

client_id;YOUR_CLIENT_ID
client_secret;YOUR_SECRET_ID
scope;https://www.googleapis.com/auth/drive

By default, the scope defined in this repo is the following:

https://www.googleapis.com/auth/plus.login

Basic Overview

enter image description here

Usage

If GoogleContexts.csv is well filled with client_id, client_secret and scope. Then you can execute this job. You will receive for the first time a message saying the following:

[statistics] connecting to socket on port 3466
[statistics] connected
Refresh token and access token not found.
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/plus.login&state=123456789qwertyui&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=441071126182-kg470tgvp2vr14jk259of6ap0lkh728s.apps.googleusercontent.com&approval_prompt=auto&include_granted_scopes=true&access_type=offline

URL has been generated according to your client id and scopes you defined. The process is the following:

  • Copy paste URL on a browser and get authorization
  • Copy paste authorization code and put in Talend in message box : enter image description here

Once it's done, it will automatically get an access token and made it available in GoogleContexts.csv. Nothing else. You can see the result in GoogleContexts.csv that is looking like that now:

client_secret;zd0-dKMjrODHFFAs8leXiNQ4
redirect_uri;urn:ietf:wg:oauth:2.0:oob
refresh_token;1/ccXLxSshiUWtFPdZV5AZbZG_IuqQzF5q3X7ASAs_QPE
scope;https://www.googleapis.com/auth/plus.login
code;
path;C:/talend/
client_id;441071126182-kg470tgvp2vr14jk259of6ap0lkh728s.apps.googleusercontent.com
access_token;ya29.GltkBTQch8ubkWSHzXN911G4XCCZpaX0AsYuHctRCkJmwbizyCwMuKihWRPcOCTxp7tvvVnE2b09lh_8k5CK5B3MOvbVsZZCT_25mElv5aBRKeI0AZXBJ0TYhFXi

There is no manual operations required anymore. Every time you execute the job, it will update GoogleContexts.csv with an updated access_token.

Let's see two examples below to see how to use in other jobs.

Example 1 getting Gmail metadata

Available in the following repo: get_gmail_metadata_talend

Example 2 getting Calendar resources

Available in the following repo: get_calendar_resources_talend

Tips and tricks

Updating required scopes

You can add several scopes separated by a space in GoogleContexts.csv file:

scope;https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/drive.readonly

If you do that, then you also need to remove access_token and refresh_token fields from GoogleContexts.csv. Or redownload GoogleContexts.csv from scratch and fill client_id, client_secret and scope as defined in Configure GoogleContexts.csv section.

Then the job will ask you to re authorize your app to access data stored in your Google account. Your access token will have authorization to request on Google APIs according to the scopes you defined and re authorized. Otherwise, request will be forbidden.

Revoking access to a token or application

At any time, you can revoke access of GetGoogleToken at this url: https://accounts.google.com/issuedauthsubtokens

About

This repository contains the Talend job GetGoogleToken. The goal is to make sure we always have an access token ready to be used to call any Google endpoints in Talend. Can be re used in other jobs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published