简体   繁体   中英

Google drive API: can't upload files to the root folder using nodejs

I'm using google drive API to upload files. It works if I specify the id of subfolders. But when I pass the id of the root folder, the files are not uploaded. Here is my request:

      drive.files.create(
    {
      auth: this.ggToken,
      fields: 'id',
      supportsAllDrives: true,
      media: {
        body: this.convertBufferToStream(file.buffer),
      },
      requestBody: {
        mimeType: file.mimetype,
        name: file.originalname,
        parents: ['root', '0AASRHiHHtzxrUk9PVA'],
      },
    },
    (e: Error, f: any) => {
      if (e) {
        console.error(e);
      }
      console.log(f);
    },
  );

0AASRHiHHtzxrUk9PVA is the ID of the root folder (I get it by using drive.files.get API) So what's wrong with my code? How can I upload files to the root folder? Thanks.

Update 1 Here is my script:

      ggToken: JWT;

  constructor() {
    this.ggToken = new google.auth.JWT(
      process.env.GG_DRIVE_CLIENT_EMAIL,
      null,
      process.env.GG_DRIVE_PRIVATE_KEY,
      ['https://www.googleapis.com/auth/drive'],
      null,
    );
  }

  async uploadFiles(file: any) {
    const credentials = await this.ggToken.authorize();
    this.ggToken.setCredentials(credentials);
    const uploadedFile = await drive.files.create({
      auth: this.ggToken,
      fields:
        'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
      media: {
        body: this.convertBufferToStream(file.buffer),
      },
      requestBody: {
        mimeType: file.mimetype,
        name: file.originalname,
        parents: ['root'],
        properties: {},
      },
    });
    return {
      driveResource: uploadedFile.data,
    };
  }

I got process.env.GG_DRIVE_CLIENT_EMAIL and process.env.GG_DRIVE_PRIVATE_KEY from the json file after creating server account key.

Thank @Tanaike very much. Here is his answer:

Thank you for replying and adding the script. I could understand about the reason of your issue. The service account is different from your own account. So the Google Drive of service account is different from that of your account. By this, when a file is uploaded to the root folder using your script, the file is uploaded to the root folder of the Drive of service account. By this, the file cannot be seen at the drive of your account. If you want to upload the file to the root folder of the Drive of your account, please use OAuth2

With Oauth2, I can upload files to the root folder. Here is my code:

      oauth2Client: OAuth2Client;

  constructor() {
    this.oauth2Client = new google.auth.OAuth2({
      clientId: process.env.CLIENT_ID,
      clientSecret: process.env.CLIENT_SECRET,
      redirectUri: 'https://developers.google.com/oauthplayground',
    });
    this.oauth2Client.setCredentials({
      access_token: process.env.ACCESS_TOKEN,
      refresh_token: process.env.REFRESH_TOKEN,
    });
  }

  async uploadFiles(file: any) {
    const uploadedFile = await drive.files.create({
      auth: this.oauth2Client,
      fields:
        'id, name, mimeType, webViewLink, webContentLink, iconLink, size, originalFilename',
      media: {
        body: this.convertBufferToStream(file.buffer),
      },
      requestBody: {
        mimeType: file.mimetype,
        name: file.originalname,
        parents: ['root'],
      },
    });
    return {
      driveResource: uploadedFile.data,
    };
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM