繁体   English   中英

如何在流星上使用Google Drive API上传文件?

[英]How do I upload files via using Google Drive API on meteor?

我正在使用Meteor框架来实现Google Drive API。 我已经生成了clientId,clientSecret和redirectUrl。

在这种方法中,我必须获取该URL,并且当我单击“允许”按钮时,它是我在重定向URL中指定的重定向URL。 它在url中提供了代码,我已经保存了该代码。

 checkForAuthorization = function() {
   return new Promise(function(resolve, reject) {
   console.log("checkForAuthorization method is running......");
   var clientId, clientSecret, redirectUrl, oauth2Client;
   clientId = "XYZ";
   clientSecret = "ABC";
   redirectUrl = "http://localhost:3000/home";
   oauth2Client = new google.auth.OAuth2(clientId, clientSecret, 
      redirectUrl);
   getGoogleDriveAccessToken(oauth2Client).then(function(result) {
     resolve(result);
   }).catch(function(error) {
    reject();    
   });
   });
  };

该代码用于上传文件。 登录名给我一个错误,提示需要登录。

var uploadFileOnGoogleDrive = function(token) {
    return new Promise(function(resolve, reject) {
         var fileMetadata = {
             'name': 'Claremont-Coral-Pillow-12x241.jpeg'
         };
         var media = {
               mimeType: 'image/jpeg',
               body: fs.createReadStream
                ('/home/administrator/Pictures/Claremont- 
                  Coral-Pillow-12x241.jpeg')
          };
         drive.files.create({
                    auth: token,
                    resource: fileMetadata,
                    media: media,
                    fields: 'id'
             }, function (err, file) {
             if (err) {
                  console.log("The error is ", err);      
             } else {
                  console.log('File Id: ', file.id);
             }
           });
         });
      };

我究竟做错了什么?

您正在使用仅用于终端的代码(命令提示符)。 正如文档在下面的链接中所说的那样

https://developers.google.com/drive/api/v3/quickstart/nodejs

此示例中的授权流是为命令行应用程序设计的。 有关如何在其他上下文中执行授权的信息,请参阅授权和身份验证。 图书馆自述文件中的部分。

为了获得答案,您应该仔细阅读并使用下面的API文档链接中提供的代码:-

https://github.com/google/google-api-nodejs-client/#authorizing-and-authenticating

谢谢

确保已遵循适用于所有应用程序的OAuth 2.0常规流程。

  1. 创建应用程序时,您可以使用Google API控制台进行注册。 然后,Google提供您稍后需要的信息,例如客户ID和客户机密。
  2. 在Google API控制台中激活Drive API。 (如果API控制台中未列出该API,请跳过此步骤。)
  3. 当您的应用程序需要访问用户数据时,它会询问Google特定的访问范围。
  4. Google向用户显示一个同意屏幕,要求他们授权您的应用程序请求其某些数据。
  5. 如果用户批准,则Google会为您的应用提供一个短暂的访问令牌。
  6. 您的应用程序请求用户数据,并将访问令牌附加到请求。 如果Google确定您的请求和令牌有效,则会返回请求的数据。

有关其他参考,您可以按照此SO post进行操作

由于版本问题不支持高于25.0.0.1,我在使用googleapis与JavaScript集成时遇到了相同的问题,但是当我像这样传递凭据时,它解决了我的问题

function uploadFile(tmp_path,_folderID,file_name,contentType,cb){

  var  __fileData = fs.createReadStream(tmp_path);
   var OAuth2 = google.auth.OAuth2;
            var oauth2Client = new OAuth2(
                    client_id,
                    secretKey,
                    redirect_url
                    );
            // Retrieve tokens via token exchange explained above or set them:
            oauth2Client.credentials = {
                access_token: body.access_token,
                refresh_token: refreshToken
            };
            var drive = google.drive({version: 'v3', auth: oauth2Client});
            drive.files.create({
                resource: {
                    name: file_name,
                    parents: _folderID,
                    mimeType: contentType
                },
                media: {
                    mimeType: contentType,
                    body: __fileData
                }
            }, function (err, response) {
               if(err) cb(err,null);
                cb(null,'success');

    })
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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