I’m having trouble with the Google Docs API. I’ve set up a project, enabled the API, and created a service account. But when I try to read a file, I get a 404 error even though the file exists.
Here’s what I’ve done:
- Set up a Google Cloud project
- Turned on the Docs API
- Made a service account with full access
- Got the JSON key for the account
I’m using Node.js with the googleapis package. Here’s a simplified version of my code:
const { google } = require('googleapis')
const auth = new google.auth.GoogleAuth({
keyFile: 'my-key.json',
scopes: ['https://www.googleapis.com/auth/documents']
})
async function readDoc(docId) {
const client = await auth.getClient()
const docs = google.docs({ version: 'v1', auth: client })
return docs.documents.get({ documentId: docId })
}
readDoc('my-doc-id').then(console.log).catch(console.error)
When I run this, I get a 404 error saying the document wasn’t found. But I can open the file in my browser just fine. What am I doing wrong? Is there a step I missed or a permission I need to set?
I’ve encountered this problem as well. One crucial step that’s often overlooked is domain-wide delegation. If you’re working with a Google Workspace account, you need to set up domain-wide delegation for your service account.
To do this, go to your Google Workspace admin console, navigate to Security > Access and data control > API controls. Click on ‘Manage Domain Wide Delegation’, then add your service account’s client ID and authorize it for the https://www.googleapis.com/auth/documents scope.
Also, ensure you’re using the correct document ID. It’s the long string in the URL between /d/ and /edit when you open the document in a browser.
If these steps don’t resolve the issue, try using a refresh token instead of a service account. This sometimes works when dealing with tricky permission scenarios.
hey mate, i had the same issue. turns out the service account needs explicit permission for each doc. try sharing the document with the service account email (ends with .iam.gserviceaccount.com). give it viewer access at least. that should fix it. good luck!
I’ve run into this issue before, and it’s usually related to permissions. Even though your service account has full access, it might not have explicit permission to view that specific document.
Here’s what worked for me:
- Open the Google Doc in your browser.
- Click the ‘Share’ button in the top right.
- In the ‘Share with people and groups’ field, paste the email address of your service account. It should look something like ‘[email protected]’.
- Give it at least ‘Viewer’ access.
After doing this, try running your code again. It should work now.
Also, double-check that the document ID you’re using is correct. It’s the long string of characters in the URL when you have the document open in your browser.
If it still doesn’t work, you might want to try using a different scope. Instead of ‘https://www.googleapis.com/auth/documents’, try ‘https://www.googleapis.com/auth/drive.readonly’ or ‘https://www.googleapis.com/auth/drive.file’.
Hope this helps!