Skip to content
Advertisement

Google Drive API: How to create a file in appDataFolder?

I’m reading this documenation: https://developers.google.com/drive/api/v3/appdata

This is my code:

var fileMetadata = {
    'name': 'config.json',
    'parents': ['appDataFolder']
};
var media = {
    mimeType: 'application/json',
    body: '"sample text"'
};
const request = gapi.client.drive.files.create({
    resource: fileMetadata,
    media,
    fields: 'id'
})
request.execute(function (err, file) {
    if (err) {
        // Handle error
        console.error(err);
    } else {
        console.log('Folder Id:', file.id);
    }
})

I get a 403 error: “The user does not have sufficient permissions for this file.”

Does not the user have permission to create a file in his appDataFolder? How to create a file in it?

The scope of gapi client is ‘https://www.googleapis.com/auth/drive.appdata’ and the user accepted it.

Advertisement

Answer

I believe the reason for this error is that you are only using the scope to access the appdata folder, but not the scope to create files. Accessing the app data folder and creating files are two different things. According to your code, you are trying to create a file in the appdata folder.

I suggest you to include both scopes:

https://www.googleapis.com/auth/drive.appdata
https://www.googleapis.com/auth/drive.file

If you are not using incremental authorization, make sure to revoke access and reauthorize again.

Reference: https://developers.google.com/drive/api/v3/about-auth#OAuth2Authorizing

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement