简体   繁体   English

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

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

I'm using the Meteor framework to implement Google Drive API. 我正在使用Meteor框架来实现Google Drive API。 I have generated clientId, clientSecret and redirectUrl. 我已经生成了clientId,clientSecret和redirectUrl。

In this method I have to get the url and when I clicked the Allow button, its redirect url which I have given in the redirect url. 在这种方法中,我必须获取该URL,并且当我单击“允许”按钮时,它是我在重定向URL中指定的重定向URL。 It's give the code in the url, I have save that code. 它在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();    
   });
   });
  };

This code is for uploading the file. 该代码用于上传文件。 The login gives me an error saying that a login is required. 登录名给我一个错误,提示需要登录。

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);
             }
           });
         });
      };

What am I doing wrong? 我究竟做错了什么?

You are using the code that is made only for the terminal (command prompt). 您正在使用仅用于终端的代码(命令提示符)。 As the documentation says itself on the below link 正如文档在下面的链接中所说的那样

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

The authorization flow in this example is designed for a command line application. 此示例中的授权流是为命令行应用程序设计的。 For information on how to perform authorization in other contexts, see the Authorizing and Authenticating. 有关如何在其他上下文中执行授权的信息,请参阅授权和身份验证。 section of the library's README. 图书馆自述文件中的部分。

For your answer, you should go through and use the code given in the below API Doc link:- 为了获得答案,您应该仔细阅读并使用下面的API文档链接中提供的代码:-

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

Thanks 谢谢

Make sure you have followed the OAuth 2.0 general process which is applicable to all applications. 确保已遵循适用于所有应用程序的OAuth 2.0常规流程。

  1. When you create your application, you register it using the Google API Console. 创建应用程序时,您可以使用Google API控制台进行注册。 Google then provides information you'll need later, such as a client ID and a client secret. 然后,Google提供您稍后需要的信息,例如客户ID和客户机密。
  2. Activate the Drive API in the Google API Console. 在Google API控制台中激活Drive API。 (If the API isn't listed in the API Console, then skip this step.) (如果API控制台中未列出该API,请跳过此步骤。)
  3. When your application needs access to user data, it asks Google for a particular scope of access. 当您的应用程序需要访问用户数据时,它会询问Google特定的访问范围。
  4. Google displays a consent screen to the user, asking them to authorize your application to request some of their data. Google向用户显示一个同意屏幕,要求他们授权您的应用程序请求其某些数据。
  5. If the user approves, then Google gives your application a short-lived access token. 如果用户批准,则Google会为您的应用提供一个短暂的访问令牌。
  6. Your application requests user data, attaching the access token to the request. 您的应用程序请求用户数据,并将访问令牌附加到请求。 If Google determines that your request and the token are valid, it returns the requested data. 如果Google确定您的请求和令牌有效,则会返回请求的数据。

For additional reference, you can follow this SO post . 有关其他参考,您可以按照此SO post进行操作

I had same problem integrating with JavaScript using googleapis due to version issues not supported greater than 25.0.0.1 but when I was passing credential like this it resolved my problem 由于版本问题不支持高于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.

相关问题 使用Google Drive API上传大文件 - Using Google Drive API to upload large files 如何使用谷歌驱动器 api 将文件上传到谷歌驱动器? - How I can upload file to google drive with google drive api? 如何通过Google Drive API(NodeJS)上传Google Apps脚本 - How to Upload a Google Apps Script via the Google Drive API (NodeJS) 结合使用Meteor Google Auth和Google Drive API - Using Meteor Google Auth with Google Drive API 如何使用脚本重命名谷歌驱动器中的多个文件? - How do i rename multiple files in google drive using script? 如何使用其API在Google驱动器中打开文件? - How do I open a file in google drive using its api? 如何使用Drive API创建空白的Google Spreadsheet? - How do I create a blank Google Spreadsheet using Drive API? 如何通过Google云端硬盘Javascript OAuth2 API创建Google表格文档? - How do I create a Google Sheets document via the Google Drive Javascript OAuth2 API? Google Drive API:通过 Multipart API 上传二进制文件的正确方法 - Google Drive API: Correct way to upload binary files via the Multipart API 如何使用客户端Javascript通过API将跨域文件导入Google云端硬盘? - How to get cross-origin files into Google Drive via an API using client-side Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM