0

I created a key in the google cloud console. I even tried remaking it and using a the new one.

enter image description here

Im trying to use it like so:

export const getSheet= async () => {
      try {
          const sheetId ='xxxxxxx'
          const tabName = 'myTab'
          const accountKey = 'xxxxxxx'
          const url =' https://sheets.googleapis.com/v4/spreadsheets/'+ sheetId +'/values/'+tabName+'?key='+ accountKey
          console.log(url)
          const response = await fetch(url);
        console.log(response);
        return '';
      } catch (error) {
        console.log(error);
      } finally {
        console.log('finally');
      }
    };

The request being sent is:

https://sheets.googleapis.com/v4/spreadsheets/xxxxxxx/values/myTab?key=xxxxxxx

No matter what I do I get

error: {code: 403, message: "The caller does not have permission", status: "PERMISSION_DENIED"}

Ive refered to these stack overflow posts regarding the same issue with no luck

  1. Error 403 on Google Sheets API
  2. Google Sheets API V4 403 Error
  3. Getting 403 from Google Sheets API using apikey

Has anyone come across this and was able to fix it?

Thanks -Coffee

CoffeePeddlerIntern
  • 619
  • 3
  • 12
  • 28

2 Answers2

2

You can't use an API key to access (Google Workplace) user data such as sheets; you may (!?) be able to get away with only use an API Key if the sheet were public (anyone with the link).

The options are admittedly confusing but:

  • API keys authenticate apps
  • OAuth is used to authenticate users

Have a look at authentication & authorization and OAuth for client-side web apps.

DazWilkin
  • 24,003
  • 5
  • 35
  • 71
  • yeah I tried making it public with no luck. I guess im confused on why i can make one and scope it to google sheets but it cant access google sheets? Or is there a certain type of sheets it can get to? – CoffeePeddlerIntern Jun 02 '22 at 01:29
  • I encourage you to read the links I included. Google enforces extra controls over access to user data. This requires that e.g. you formally approve the data access (to an app) before your data can be accessed. Two of the (!) challenges with API Keys is that they aren't user (credential) specific and they're eternal. – DazWilkin Jun 02 '22 at 01:35
  • I see. So what would you say the purpose of the keys are? – CoffeePeddlerIntern Jun 02 '22 at 01:59
  • I figured it out. When i was clicking "anyone with a link" All i was doing was copying it to the clipboard. I didnt realize it was another setting to with it to totally public. This data isnt sensative just a side project. So Oauth seemed like overkill. Thank you for your time! – CoffeePeddlerIntern Jun 02 '22 at 02:04
1

You can look through the Javascript Quickstart guide for Sheets API for the OAuth Setup. And you can access the sheet using Spreadsheet.values.get method similar to this sample script on the provided URL reference.

async function listMajors() {
        let response;
        try {
          // Fetch first 10 files
          response = await gapi.client.sheets.spreadsheets.values.get({
            spreadsheetId: 'SHEETID',
            range: 'SHEETRANGE',
          });
        } catch (err) {
          document.getElementById('content').innerText = err.message;
          return;
        }
        const range = response.result;
        if (!range || !range.values || range.values.length == 0) {
          document.getElementById('content').innerText = 'No values found.';
          return;
        }

And just like what DazWilkin provided. Make sure that your app complies with Google's OAuth2 policies.

You can also test your request url first on a browser and see if it returns a JSON response of the sheet data. Tested this https://sheets.googleapis.com/v4/spreadsheets/xxxxxxx/values/myTab?key=xxxxxxx format on a public sheet, and it returned the sheet data as long as it is set to 'Anyone with the link'

Diego Sanchez
  • 541
  • 2
  • 9
  • 1
    Holy cow. I got it. So when i was clicking "anyone with a link" All i was doing was copying it to the clipboard. I didnt realise it was another setting to with it to totally public. – CoffeePeddlerIntern Jun 02 '22 at 02:02
  • 1
    @CoffeePeddlerIntern Alright, if you're comfortable with setting the sheets privacy settings to Public then that should be all good – Diego Sanchez Jun 02 '22 at 02:06