Search code examples
google-apps-scriptoauth-2.0google-cloud-storagehttp-status-code-403google-developers-console

HTTP request works on playground doesn't in Google App scripts


I want to get data from Google Cloud through API request basically to a spreadsheet.

I am not really a coder, just an analyst who is trying to automate everything. Sorry if this is a stupid question or not the best idea to be doing in the first place. Anyways, here is the issue.

I use the https://developers.google.com/oauthplayground/ to have a look what can I get out of the google cloud files as I have just read-only access to the files there. There are Google Play data that I want to get to an automatic report somehow.

I authorize API with "https://www.googleapis.com/auth/devstorage.read_only" scope, then go through the 'Exchange authorization code for tokens' and then I can have a look both at the metadata and at the file itself: This gives me meta data:
https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_0000000000000000/o/reviews%2Freviews_com.appid_201909.csv

Adding "?generation=1569963312859248&alt=media" shows me the contents of the csv.

I wanted to make this to automatically get the data to somewhere easiest seemed to get it to a spreadsheet. So I made some code in Google App script. Code is below. I added the scope in manifest file too.

var token = ScriptApp.getOAuthToken();
var options = {};
  options.headers = {"Authorization": "Bearer "+token};
  var links = UrlFetchApp.fetch("https://www.googleapis.com/storage/v1/b/pubsite_prod_rev_0000000000000000/o/reviews%2Freviews_com.appid_201909.csv?generation=1569963312859248&alt=media" , options);
  var data =  links.getBlob().getDataAsString('utf-16le');

The weird part is that I can get the metadata completely ok, but when I try getting the data itself I get error

"Request failed for https://www.googleapis.com returned code 403. Truncated server response: "Project 10000000000000 is not found and cannot be used for API calls. If it is recently created, enable Cloud Storage JSON API by visiting https://c..."

But how can I look at the meta data with my API call then?

Another thing I tested - I took the access token from playground and not the function and all worked then. But I really want to make this automatic. Is it something about scopes? But I do not know what.


Solution

  • 403

    usually means you haven't enabled the required api.

    To enable a API(that's not available in the advanced Google service) in the default GCP project, you need to