Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I am not able to create folder in google drive using google drive APIs #314

Open
tush009 opened this issue Nov 22, 2022 · 1 comment
Open
Assignees

Comments

@tush009
Copy link

tush009 commented Nov 22, 2022

I have been trying to create a folder with OAuth and a service account using google drive APIs in google drive. I can see that folder in the service account but not in the google drive GUI.

Here is the code which I tried to create a google drive folder

const { google } = require('googleapis');
require('dotenv').config();
const CREDENTIALS_PATH = process.env.CREDENTIALS_PATH

const scopes = ['https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive']
module.exports = {
    createFolder: async () => {
        const auth = new google.auth.GoogleAuth({
            scopes: scopes,
            keyFilename: CREDENTIALS_PATH 
        })
        const service = google.drive({ version: 'v3', auth: auth });

        const fileMetadata = {
            name: 'newFolder',
            mimeType: 'application/vnd.google-apps.folder'
        };

        try {
            const file = await service.files.create({
                requestBody: fileMetadata,
                fields: 'id',
            });

            if (!file.data.id) return;
            const res2 = await service.permissions
                .create({
                    resource: {
                        role: "owner",
                        type: "user",
                        emailAddress: email, 
                    },
                    fileId: file.data.id,
                    transferOwnership: true,
                    supportsAllDrives: true
                }).catch((err) => console.log(err));
            console.log(res2.data);

            return file.data.id;
        } catch (err) {
            throw err;
        }
    }

}

When I give permission to open a folder in drive then it throws an error.

Please find the below error:

GaxiosError: Bad Request. User message: "You cannot share this item because it has been flagged as 
  response: {
    config: {
      url: 'https://www.googleapis.com/drive/v3/files/15NJCr03yIXZ5Sv-xwK3ZiuW0UFTIk4IM/permissions?fields=%2A&transferOwnership=true&moveToNewOwnersRoot=true',
      method: 'POST',
      userAgentDirectives: [Array],
      paramsSerializer: [Function (anonymous)],
      data: [Object],
      headers: [Object],
      params: [Object],
      validateStatus: [Function (anonymous)],
      retry: true,
      body: '{"type":"user","role":"owner","emailAddress":"########"}',
      responseType: 'json',
      retryConfig: [Object]
    },
    data: { error: [Object] },
    headers: {
      'alt-svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
      'cache-control': 'private, max-age=0',
      connection: 'close',
      'content-encoding': 'gzip',
      'content-type': 'application/json; charset=UTF-8',
      server: 'ESF',
      'transfer-encoding': 'chunked',
      vary: 'Origin, X-Origin',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'SAMEORIGIN',
      'x-xss-protection': '0'
    },
    status: 400,
    statusText: 'Bad Request',
    request: {
      responseURL: 'https://www.googleapis.com/drive/v3/files/15NJCr03yIXZ5Sv-xwK3ZiuW0UFTIk4IM/permissions?fields=%2A&transferOwnership=true&moveToNewOwnersRoot=true'
    }
  },
  config: {
    url: 'https://www.googleapis.com/drive/v3/files/15NJCr03yIXZ5Sv-xwK3ZiuW0UFTIk4IM/permissions?fields=%2A&transferOwnership=true&moveToNewOwnersRoot=true',
    method: 'POST',
    userAgentDirectives: [ [Object] ],
    paramsSerializer: [Function (anonymous)],
    data: {
      type: 'user',
      role: 'owner',
      emailAddress: '#########'
    },
    headers: {
      'x-goog-api-client': 'gdcl/6.0.3 gl-node/18.5.0 auth/8.7.0',
      'Accept-Encoding': 'gzip',
      'User-Agent': 'google-api-nodejs-client/6.0.3 (gzip)',
      Authorization: 'Bearer #############################################',
      'Content-Type': 'application/json',
      Accept: 'application/json'
    },
    params: { fields: '*', transferOwnership: true, moveToNewOwnersRoot: true },
    validateStatus: [Function (anonymous)],
    retry: true,
    body: '{"type":"user","role":"owner","emailAddress":"##############"}',
    responseType: 'json',
    retryConfig: {
      currentRetryAttempt: 0,
      retry: 3,
      httpMethodsToRetry: [Array],
      noResponseRetries: 2,
      statusCodesToRetry: [Array]
    }
  },
  code: 400,
  errors: [
    {
      message: 'Bad Request. User message: "You cannot share this item because it has been flagged as inappropriate."',
      domain: 'global',
      reason: 'invalidSharingRequest'
    }
  ]
}

Can you please help me to find that folder in google drive using google drive API?

@tush009 tush009 changed the title Folders are not created in Google Drive by Node.js code I am not able to create folder in google drive using google drive APIs Nov 24, 2022
@ajahuanex
Copy link

const file = await service.files.create({ requestBody: fileMetadata, fields: 'id', supportsAllDrives: true, });

add supportsAllDrives: true to enable shared, but you should administrator permission or created this file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants