I’m trying to fetch folder details from Google Drive API, but I’m running into a problem. I’ve set up my request to get the id, name, and parents of a folder. Here’s a simplified version of my code:
async function getFolderInfo(folderId) {
const result = await fetch(`https://drive.googleapis.com/v3/files/${folderId}?fields=id,name,parents&key=${apiKey}`);
return result.json();
}
The API gives me the id and name, but the parents field is missing. I’ve done the following:
- Turned on the Drive API
- Made an API key (it’s not restricted)
- Set folder permissions to ‘Anyone with link can view’
Any ideas why I’m not getting the parents info? I’m stumped and could use some help. Thanks!
I’ve encountered a similar issue before, and it turns out the problem might be with your authorization method. Using an API key alone isn’t sufficient for accessing certain file metadata, including parent information. Instead, you should use OAuth 2.0 for authentication.
Try modifying your approach to use OAuth 2.0 and include an access token in your request. You’ll need to set up OAuth consent screen in Google Cloud Console, create OAuth 2.0 credentials, and implement the OAuth flow in your application.
Once you have the access token, your request should look something like this:
const result = await fetch(`https://www.googleapis.com/drive/v3/files/${folderId}?fields=id,name,parents`, {
headers: {
'Authorization': `Bearer ${accessToken}`
}
});
This should resolve the issue and allow you to retrieve the parent folder information. Remember to request the necessary scopes during the OAuth process, such as ‘https://www.googleapis.com/auth/drive.metadata.readonly’.
I’ve dealt with this exact problem before, and it can be really frustrating. The key thing to remember is that the Google Drive API treats ‘parents’ as sensitive information, so it won’t return them with just an API key. What helped me was switching to OAuth 2.0 instead. Although it’s a bit more complex to set up, it’s essential for accessing this type of data. Also, make sure you request the appropriate scope, such as ‘https://www.googleapis.com/auth/drive.metadata.readonly’. Finally, ensure your API call explicitly asks for the ‘parents’ field, since the API may not return it unless specified. This approach made a difference for me.