๐Ÿ“ŠUsing Google API (drive & spreadsheet)

In order to be able to use Google Sheets or Drive API, you must first authenticate yourself with the service. All Google APIs use the OAuth 2.0 protocol for authentication and authorization, which simplifies the process. Moreover, you can also create a service account that can be used to access all of Googleโ€™s resources.

Setting up

Enable the Google Sheets API

  • Go to the Google APIs Console

    Click โ€œSelect a projectโ€ in the top-right corner

  • Click the โ€œNew Projectโ€ option

  • Give a unique name to your project and click โ€œCreateโ€

  • Go to the APIs dashboard

    Search for โ€œGoogle Sheets APIโ€ and click on it

  • Click โ€œEnableโ€ and wait for the API to be enabled

Create a Service Account

  • When the API is enabled, move to the Credentials page

    Click the โ€œCreate credentialsโ€ option and select โ€œService Accountโ€

  • Give a name to the service account and click โ€œCreateโ€

  • Click โ€œSelect a roleโ€ => โ€œProjectโ€ => โ€œEditorโ€

  • Click โ€œDoneโ€

    Now that your service account has been created, you will have to create a key, which will allow you to connect to the API automatically via this service account.

Create a Service Key

  • In the Credentials page, click on your service account name

  • Go to โ€œKeysโ€

  • Select โ€œAdd Keyโ€ => โ€œCreate new keyโ€

  • Leave the option as JSON and click โ€œCreateโ€

  • Rename the downloaded file to client_secret.jsonThe file contains all the sensitive information that will allow your app to authenticate with Google and have access to the API. Itโ€™s critical for this file to be kept private so that only your application has access to it. The file will look like this:

    Find the โ€œclient_emailโ€ value and copy the email address. Each spreadsheet that you want to be manipulated by your app must provide access to this email.

Share your spreadsheet with client email

  • Click โ€œShareโ€ in the top-right corner of your spreadsheet.

  • Use client_email from the client_secret.json. Give Editor right. Click "Send".

Now your service account has Edit access to the sheet and your application can use Google Sheets API to access the spreadsheet.

Last updated