gglukmann / use-google-sheets

📝 A React Hook for getting data from Google Sheets API v4

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The caller does not have permission

terraincognitas opened this issue · comments

Hi. I'm receiving the error "The caller does not have permission" when running this.

I've configured the app in Google Console to allow the Google Sheets API—creating an API Key and a Service Account. The API Key is used in the app as GATSBY_GOOGLE_SHEETS_API_KEY, GATSBY_GOOGLE_SHEETS_SHEET_ID added from the Sheet URL, and the Service Account email was added to the Google Sheet with full Editor permissions.

I'm not entirely sure which apiKey value to set in the application env variable. Where is the correct API Key found/created? Is it created in the Service Account, the general Credentials settings in Console, or one of the values in the json file credentials?

Application Code (typescript)

const { data, loading, error } = useGoogleSheets({
    apiKey: process.env.GATSBY_GOOGLE_SHEETS_API_KEY as string,
    sheetId: process.env.GATSBY_GOOGLE_SHEETS_SHEET_ID as string,
  });

Console error:

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

I tracked this down to being a result of the Google Sheet having general sharing access set to Restricted. It worked when the sheet was temporarily made Public.

How do I allow read/write access to this sheet but keep the sheet sharing to Restricted?