-1

I have integrated the google drive download and share the file in my website.

A user login in our application with the google-login, we got his authorise scope once he logged-in our website. We are integrated google-chooser for pickup any file from his personal google drive. once he chosen any file eg: sample.pdf we are downloading his file from google drive to our website.we are using below code.

function downloadFile(auth)
{
  const drive = google.drive({ version: 'v3', auth });
  var fileId = 'Choosen-file-id from google drive';
  const dest = fs.createWriteStream('./downloadFiles/12.doc');
  try{
        drive.files.export({
            fileId: fileId,
            mimeType: 'application/vnd.google-apps.document'
        }, {
            responseType: 'stream'
        },function(err, response){
            if(err)return console.log(err);

            response.data.on('error', err => {
                console.log(err);
            }).on('end', ()=>{
                console.log('success');
            })
            .pipe(dest);
       });
      }catch(errr)
      {
        console.log(errr,'downloadFileErr')
      }
}

similarly we are sharing any file from our website to logged-in user drive account with below code.

function uploadFile(auth)
{
  const drive = google.drive({ version: 'v3', auth });  
  drive.files.create({
      headers: {
            'Authorization': 'Bearer Authorize Token received when user loggedIn with google account',
            'Content-Type': "image/png",
      },
      resource: {
        'name': `sampledocc`
      },
      media:  {
        mimeType:"image/png",
        body: fs.createReadStream('./downloadFiles/4.png')
      },
      fields: 'id'
    }, async function (err, file)  {
      if (err)
      {
        // Handle error
        console.log(err,'Failed to upload');
      } else {
        console.log(err,'File uploaded successfully');
      }
    });
}

But we are getting below errors for both sharing and downloading file.

1)Limit exceeded 2)Unauthorized access 3)You have no-permissions 4)Invalid Credentials

We are already added billing details in google cloud account and activated Google Picker API, Google Drive Api and redirection urls and non restricted scope like https://www.googleapis.com/auth/drive.file

But unable to resolve the issue. we asked google oauth team but they are mentioned ask in forums /help. so i posted this in forum. can you please share or sort out the issue.

Serhii Rohoza
  • 1,354
  • 2
  • 4
  • 14

1 Answers1

1

The invalid credentials you are experiencing are most likely due to an expired access token. You can try refreshing your access token using the long-lived refresh token or directing your user through the OAuth flow.

For permissions it could be that the user does not have edit access to the file they are attempting to access. You can check user access levels in the metadata retrieved by files.get and display a read-only UI when permissions are missing.

For your limit exceeded error it could be many different quotas for your situation I would recommend looking into this troubleshooting doc. You may need to request a quota increase or change your method of requests.

Gustavo
  • 31
  • 4