Getting 404 not found error when accessing Google Docs document through API despite file being available

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?

it seems like the doc ain’t shared with the service account. make sure to add the service account’s email to the sharing settings of the doc. also, double-check those permissions to be sure!

Had this exact problem a few months ago - drove me nuts for hours. Your document ID looks suspicious. It’s way too long. Regular doc IDs are around 44 characters, but yours is much longer. This usually happens when you copy the whole URL instead of just the ID part. Open your doc in the browser and grab the ID between ‘/document/d/’ and ‘/edit’ in the URL. That’ll give you the right format. Also, make sure your service account has viewer access to the specific document, not just API permissions.

That 404 error means it’s a sharing issue, not authentication. Your service account has its own email address and needs explicit access to the doc. Go to your Google Doc, hit Share, and add your service account’s email (looks like [email protected]) with Viewer permissions or higher. Without this, the API can’t see the document at all - hence the 404. Your code looks fine, so you’re probably just missing this sharing step. I’ve seen this catch tons of developers because people forget service accounts are completely separate from your personal Google account.