Home > Back-end >  Connecting BigQuery from GAS with Request Headers without Urlfetchapp
Connecting BigQuery from GAS with Request Headers without Urlfetchapp

Time:07-05

I want to use "Http Request Headers" to connect to BigQuery without using Urlfetchapp.

I want to authenticate my service account with the BigQuery-specific library that AppsScript provides.

I want to rewrite this code to accept headers(without using Urlfetchapp)

BigQuery.Jobs.query({
  query: " SOME QUERY; " ,
  useLegacySql: false      
}, projectID);

This page has the specs, but I couldn't figure it out.

https://developers.google.com/apps-script/guides/services/advanced#how_method_signatures_are_determined

CodePudding user response:

Unfortunately, that's not possible. Both the built-in and advanced services in Google Apps Script leverage user-specific account credentials. There is no way to modify those services to use a service account.

To use service accounts, you'll need to generate those credentials yourself (JSON Web Token keys) and use them to create access tokens to authorize your queries against the BigQuery API (via UrlFetchApp.fetch()).

However, Eric Koleda's OAuth2 library does support the two-legged OAuth flow required by service accounts and therefore removes the tedium of implementing it for yourself.

Here's a link to the documentation on how to use set-up a two-legged OAuth flow for service accounts using that library:

That should be enough to get you moving in the right direction.

CodePudding user response:

URLfetchapp is used to issue HTTP and HTTPS request and response headers, so to connect BigQuery with GAS with request headers, URLFetchApp has to be used using URLFetchApp.fetch method as mentioned in the documentation.

Moreover for authenticating the service accounts, you can use OAuth2 for Apps Scripts which uses access tokens for authorization. You can check the setup process in this link. For using OAuth2 with a service account, you need to create a service account and also need to create a JWT(JSON Web Token). You can request an access token from Google which can be used to make API calls. For more information, you can check this documentation.

  • Related