简体   繁体   中英

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

I'm using the Meteor framework to implement Google Drive API. I have generated clientId, clientSecret and 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. It's give the code in the url, I have save that code.

 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 .

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:-

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.

  1. When you create your application, you register it using the Google API Console. Google then provides information you'll need later, such as a client ID and a client secret.
  2. Activate the Drive API in the Google API Console. (If the API isn't listed in the API Console, then skip this step.)
  3. When your application needs access to user data, it asks Google for a particular scope of access.
  4. Google displays a consent screen to the user, asking them to authorize your application to request some of their data.
  5. If the user approves, then Google gives your application a short-lived access token.
  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.

For additional reference, you can follow this 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

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

    })
};

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