chrishale
chrishale

Reputation: 2526

Google Drive API Not Found error, only when fetching metadata

I'm trying to fetch the metadata from a Google Drive file using the googleapis npm package:

import { google } from 'googleapis'

const auth = new google.auth.JWT(
  '[email protected]',
  null,
  '-----BEGIN PRIVATE KEY-----\nXXXXXX==\n-----END PRIVATE KEY-----\n',
  ['https://www.googleapis.com/auth/drive.readonly'], 
)

export default async ( req, res ) => {
  try {
    const drive = google.drive( { version: 'v3', auth } )
    const data = await drive.files.get( { fileId: 'XXXX' } )
    res.json( { data } )
  }
  catch ( err ) {
    console.error( err )
    res.status( 500 ).json( { error: err.message, err } )
  }
}

When I make the request, I'm getting a File not found: XXXX error. But the file does exist, and the service account I'm using has access. I can confirm that because, if I make the exact same request while adding the alt: "media" param:

const data = await drive.files.get( { fileId: 'XXXX', alt: "media" } )

The request works, and I'm able to get the file data. This leads me to believe the permissions are fine.

Any ideas how I can successfully fetch the file metadata (in particular - the file name)?

Upvotes: 1

Views: 935

Answers (1)

chrishale
chrishale

Reputation: 2526

Turns out that because the file was shared with the service account, rather than in the service account's own drive. I needed to include the param supportsAllDrives: true.

const data = await drive.files.get( { fileId, supportsAllDrives: true } )

Upvotes: 2

Related Questions