I am building a Google Chrome Extension. The extension can already 
successfully let a user sign into their Google account, and this allows me 
to retrieve a token to access the Google Sheets API. In particular, the 
following piece of code already works successfully:

function createSheet() {
  const url = "https://sheets.googleapis.com/v4/spreadsheets";
  fetch(url, {
  method: 'POST',
  headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer " + TOKEN
  },
  body: JSON.stringify({})
}).then((result) => {
  return result.json();
}).then((result) => {
  console.log("Values retrieved", result);
}).catch((error) => {
  console.log("error");
})
}

Here, TOKEN is the token I obtain when the user signs in. This code creates 
a new, empty spreadsheet on the account with which the user signed in. The 
code is essentially taken from the last answer to this Stackoverflow 
question 
<https://stackoverflow.com/questions/65625854/how-to-integrate-gapi-in-chrome-extensions-manifest-v3/74358255#74358255>.
 
A more general expose on how to access the Google Sheets API from a Chrome 
Extension can be found in this blog article 
<https://bumbu.me/gapi-in-chrome-extension> (these are two main resources I 
have used so far). The main obstacle in accessing the Sheets API through a 
Chrome extension is that the Chrome extensions cannot directly include 
gapi, i.e., I cannot include a statement such as

<script src="https://apis.google.com/js/client.js?onload=onGAPILoad
"></script>

in any of the HTML files of my extension. This is due to the new security 
policies of Chrome Extensions. Accordingly, I have to access the Sheets API 
as above, through a fetch function. However, I have not managed to make 
this work with any of the API methods except for the one above, where I 
simply create a new sheet. In particular, the following function does not 
work for me:

function readSheet() {
  const range = "R1C1:R2C2"
  const url = "https://sheets.googleapis.com/v4/spreadsheets/"; + 
spreadsheetId + "/values/" + range
  fetch(url, {
  method: 'POST',
  headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer " + TOKEN
  },
  body: JSON.stringify({})
}).then((result) => {
  return result.json();
}).then((result) => {
  console.log("Values retrieved", result);
}).catch((error) => {
  console.log("error");
})
}

Again,  TOKEN is the token I receive when the user signs into their Google 
Account, and spreadsheetId is now the id of the spreadsheet I want to read. 
[The id can be found in the URL through which I can open a spreadsheet, 
i.e., https://docs.google.com/spreadsheets/d/1{spreadsheetId}/edit#gid=0
opens the spreadsheet with id spreadsheetId.] However, this code does not 
work for me. When I execute it, the following error message appears in the 
console: 
*Failed to load resource: the server responded with a status of 404 ()*
And the source of the error is given as:
*https://sheets.googleapis.com/v4/spreadsheets/1_hBN2GARLwAXdole1rOtRYDUJ2XR6jjzcViulGIKY5c*

Opening this link, I get the following result:
{ "error": { "code": 403, "message": "The request is missing a valid API 
key.", "status": "PERMISSION_DENIED" } }

Can someone point out to me why I cannot access the Google Sheets API as I 
try above?

-- 
You received this message because you are subscribed to the Google Groups 
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-spreadsheets-api+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-spreadsheets-api/45fdcf78-7313-4579-9c5c-4f4490f955f6n%40googlegroups.com.

Reply via email to