Question

I'm trying to run a Google Apps Script that will load JSON data into Big Query, and am running into a problem.

I am actually trying to run this in Adwords Scripts, which are the same as Google Apps Scripts, except they don't have advanced services available, like that of the Big Query Service - so I can't use that.

They do have an OauthConfig service and you can send POST data from AdWords, so I can send data that way, but I am having trouble getting the OAuthConfig service to authenticate first.

Here is what I am running in a Google App Script:

var oAuthConfig = UrlFetchApp.addOAuthService("bigquery");
oAuthConfig.setAuthorizationUrl('https://accounts.google.com/o/oauth2/auth');
oAuthConfig.setAccessTokenUrl('https://accounts.google.com/o/oauth2/token');
oAuthConfig.setRequestTokenUrl('https://accounts.google.com/o/oauth2/token?scope=https://www.googleapis.com/auth/bigquery');
oAuthConfig.setConsumerSecret(ClientSecret);
oAuthConfig.setConsumerKey(ConsumerKey );

The ConsumerKey and ConsumerSecret are coming from Cloud Console -> APIs & Auth -> Credentials, where I set up a client ID for web application. The ConsumerKey is set to be the Client ID (something.apps.googleusercontent.com), and the ClientSecret is the Client Secret. Big Query API is enabled for the cloud console project, and I verified that I am able to upload data from other sources.

When I try to execute the following, I keep getting an authentication error:

var config = {
  'method':'post',
  'oAuthServiceName':'bigquery',
  'oAuthUseToken' : "always",
  'muteHttpExceptions':true
}

var result = UrlFetchApp.fetch('https://www.googleapis.com/upload/bigquery/v2/projects/'+projectName+'/jobs', config);

"Failed to authenticate for service: bigquery"

I understand I should pass data to it in the body, but right now I am just trying to authenticate with the service first.

Was it helpful?

Solution

In Apps Script oAuthConfig won't work to authenticate with APIs that use OAuth 2.0. You will want to build and use an OAuth 2.0 web application flow as discribed in this post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top