【问题标题】:How do I upload files via using Google Drive API on meteor?如何通过在流星上使用 Google Drive API 上传文件?
【发布时间】:2018-06-26 13:50:22
【问题描述】:

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

在这种方法中,我必须获取 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);
             }
           });
         });
      };

我做错了什么?

【问题讨论】:

    标签: javascript meteor google-drive-api google-drive-realtime-api


    【解决方案1】:

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

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

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

    对于您的答案,您应该仔细阅读并使用以下 API 文档链接中给出的代码:-

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

    谢谢

    【讨论】:

    • 谢谢。让我检查一下。
    【解决方案2】:

    确保您已遵循适用于所有应用程序的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

    【讨论】:

      【解决方案3】:

      由于不支持大于 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');
      
          })
      };
      

      【讨论】:

      猜你喜欢
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      相关资源
      最近更新 更多