This project uses Google Service Accounts to securely authenticate with Google APIs. Follow these steps to obtain and use your credentials:
- Visit the Google Cloud Console: Create Service Account Key page.
- From the Service account dropdown, select New service account.
- Give your service account a descriptive name (e.g., "Spreadsheet Data Reader").
- Optionally, assign a role to the service account that gives it the necessary permissions for your task (e.g., "Sheets Viewer" if you only need to read data).
- Click Create.
- In the "Grant this service account access to project" dialog, you can optionally grant more specific permissions. Be mindful of the principle of least privilege and only grant what's necessary.
- Click Continue.
- In the "Grant users access to this service account" dialog, you can add other users who should be able to manage this service account. This is optional.
- Click Done.
- On the Service Accounts page, you'll see your newly created service account.
- Click on the email address of the service account.
- Go to the Keys tab.
- Click Add Key and select Create new key.
- Choose the JSON key type.
- Click Create. Your key file will be downloaded automatically.
Important Security Note:
- Keep your JSON key file confidential! Do not share it publicly or commit it to version control. Treat it like a password.
Option A: Environment Variable
-
Rename the downloaded key file to
credentials.json
(or any name you prefer). -
Set the
GOOGLE_APPLICATION_CREDENTIALS
environment variable to the full path of the key file:export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/credentials.json"
Option B: keyFile
Property
-
In your Node.js code, provide the path to the key file when creating your
GoogleAuth
instance:const { google } = require("googleapis"); const auth = new google.auth.GoogleAuth({ keyFile: "/path/to/your/credentials.json", // Update with the actual path scopes: [ "[https://www.googleapis.com/auth/spreadsheets.readonly](https://www.googleapis.com/auth/spreadsheets.readonly)", ], // Or the necessary scopes for your tasks });