I’m working with the Google Docs API using Node.js and running into a strange issue. I can see the document exists in my browser, but when I try to fetch it through the API I keep getting a 404 error.
Here’s what I’ve done so far:
- Set up a Google Cloud project
- Enabled the Google Docs API
- Created a service account with proper permissions
- Generated a JSON key file
But when I run this code, it fails:
const { google } = require('googleapis')
const credentials = new google.auth.GoogleAuth({
keyFile: 'path/to/my/credentials.json',
scopes: ['https://www.googleapis.com/auth/documents']
})
async function fetchDocumentData(docId) {
try {
const client = await credentials.getClient()
const docsAPI = google.docs({ version: 'v1', auth: client })
const result = await docsAPI.documents.get({ documentId: docId })
return result.data
} catch (err) {
console.error('Error:', err)
}
}
(async () => {
const myDocId = '1ABCx-2vXYz3QlMNa4rb9cZYYxmt-RJK4XJf9ubL4jqdvD8mRjZn-DTFaoLgO_IZOTqpYdZLW4Z_P4ZpVC2fdh'
const data = await fetchDocumentData(myDocId)
console.log(data.body.content)
})()
The error message says ‘Requested entity was not found’ even though the document definitely exists. What could be causing this 404 response? Am I missing something with the document ID or permissions?