Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drive api - write to shared folder not found

I am using google drive api to write to a folder located in shared drive. I have created a service account and service account is added to folder with permission as 'content manager'.

However, when I try to use the api to upload file, I keep getting an error stating 'folder not found'. The same works fine when I try to create a folder onto my personal drive and add service account with 'editor' permission.

Can someone please help me if I missed something or that is as per design?

Below is sample code snippet:

google auth:

const driveauth = new google.auth.JWT(gSuiteUser, null,
   JSON.parse(gSuiteKey), [
       'https://www.googleapis.com/auth/drive',
       'https://www.googleapis.com/auth/spreadsheets',
       'https://www.googleapis.com/auth/drive.file',
       'https://www.googleapis.com/auth/drive.appdata'
   ])
const drive = google.drive({
   version: 'v3',
   auth: driveauth
});

Below is the code for uploading on google drive:

const fileMetadata = {
    'name': 'Filename',
    'mimeType': 'application/vnd.google-apps.spreadsheet',
    parents: [gSuiteFolder],
    convert: true
};
const media = {
    mimeType: 'text/csv',
    body: csvData
};
drive.files.create({
    resource: fileMetadata,
    media: media,
    fields: 'id'
}, (err, file) => {
    if (err) {
        // Handle error
        console.error(`Failure`);
        callback(err);
    } else {
        console.log('Success', file.data.id);
        callback(undefined, "done");
    }
});
like image 342
Anuraj p Avatar asked Mar 13 '26 02:03

Anuraj p


1 Answers

Turned out that we need to send additional attribute 'supportsAllDrives' as true as shown below:

drive.files.create({
    resource: fileMetadata,
    media: media,
supportsAllDrives: true,
    fields: 'id'
}, (err, file) => {
    if (err) {
        // Handle error
        console.error(`Failure`);
        callback(err);
    } else {
        console.log('Success', file.data.id);
        callback(undefined, "done");
    }
});
like image 94
Anuraj p Avatar answered Mar 14 '26 16:03

Anuraj p



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!