-->

谷歌驱动器V3。 可恢复的NodeJS上传创建在错误的目录中无标题文件(Google Drive

2019-11-05 12:40发布

嘿,我按照从驱动器快速入门指南的步骤和使用下列范围产生token.json:

const SCOPES = [
  'https://www.googleapis.com/auth/drive',
  'https://www.googleapis.com/auth/drive.appdata',
  'https://www.googleapis.com/auth/drive.file'
];

我然后运行下面的代码,试图创建/文件上传到谷歌驱动器。

    async function testing() {
  let driveFolder = 'FOLDERID';
  let res;
  try {
    const oauth2Client = new google.auth.OAuth2(
      credentials.client_id,
      credentials.client_secret,
      credentials.redirect_uris
    );
    await oauth2Client.setCredentials({
      ...tokenInfo
    });

    console.log('=== completed drive authentication', oauth2Client);
    const drive = await google.drive({
      version: 'v3',
      auth: await oauth2Client
    });
    console.log('=== Initialised Drive API');
    res = await drive.files.create({
      parameters :{
        uploadType: 'resumable'
      },
      requestBody: {
        name: 'Testing.txt',
        mimeType: 'text/plain',
        parents: [driveFolder]
      },
      media: {
        mimeType: 'text/plain',
        body: 'Hello world!!!'
      }
    });
    console.log('=== Completed Report UPLOAD:', res);
    return;
  } catch (err) {
    console.error('ERROR: \n', err);
  }
}

请求完成,没有赶上一个错误,但返回的响应是不确定的。 最后两个日志显示以下

=== completed drive authentication OAuth2Client {
  _events: [Object: null prototype] {},
  _eventsCount: 0,
  _maxListeners: undefined,
  transporter: DefaultTransporter {},
  credentials:
   { access_token:
      'ACCESS TOKEN STRING',
     refresh_token:
      'REFRESH TOKEN STRING',
     scope:
      'https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.appdata',
     token_type: 'Bearer',
     expiry_date: 1551070810157 },
  certificateCache: null,
  certificateExpiry: null,
  refreshTokenPromises: Map {},
  _clientId:'CLIENT ID STRING VALUE',
  _clientSecret: 'CLIENT SECRET VALUE',
  redirectUri: [ 'REDIRECT URL', 'http://localhost' ],
  authBaseUrl: undefined,
  tokenUrl: undefined,
  eagerRefreshThresholdMillis: 300000 }
=== Initialised Drive API
=== Completed Report UPLOAD: undefined

Answer 1:

您可以尝试使用快速入门的NodeJS从文档:

// change the scopes
        const SCOPES = [  'https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/drive.appdata',
        'https://www.googleapis.com/auth/drive.file'];

function uploadFile(auth){
    var drive = google.drive('v3');
    var fileMetadata = {
           'name': 'Some.jpg' // name of the file
        };
    var media = {
            mimeType: 'image/jpeg',
            resumable: true,
            //PATH OF THE FILE FROM YOUR COMPUTER
            body: fs.createReadStream('/usr/local/google/home/malicdemj/Desktop/blue-cyber-future-technology-concept-background_42077-488.jpg')
        };

        drive.files.create({
            auth: auth,
            resource: fileMetadata,
            media: media,
            fields: 'id'
        }, function (err, file) {
        if (err) {
            // Handle error
            console.error(err);
        } else {
            console.log('File Id: ', file.data.id);
        }
     });
  }

在我的硬盘上传文件:

另外,请检查该SO发布的更多细节。



文章来源: Google Drive v3. Nodejs Resumable Upload creates untitled file in wrong directory