Access to Google Drive through Service Account : Error: Login Required

Access to Google Drive through Service Account : Error: Login Required

const {google} = require('googleapis');
let drive = google.drive('v3');

exports.handler = async (req, res) => {
    res.set('Access-Control-Allow-Origin', '*')
      .set('Access-Control-Allow-Methods', 'POST')
    .status(200);
  var privatekey 
  var jwtClient 
  await global.admin.database().ref(`g_drive_token/OfficServiceAccount`).once('value').then((doc)=> {
    privatekey = doc.val()
    jwtClient = new google.auth.JWT(
      privatekey.client_email,
      null,
      privatekey.private_key,
      ['https://www.googleapis.com/auth/drive.file'],
      null);
      console.log(JSON.stringify(jwtClient))   
      authWithServicePrivateKey(jwtClient)
      return "null"
    }) .catch((error)=> {            
    console.log('Error fetching user data:+', error);
      return "null"
  }) 


function authWithServicePrivateKey(jwtClient){
  //authenticate request
jwtClient.authorize(function (err, tokens) {
    if (err) {
      console.log("Could not connect***!"+err);
      return;
    } else {
      console.log("Successfully connected!");
      console.log('token******'+JSON.stringify(tokens))
      listFolderInGoogleDrive()
      CreateFolderInGoogleDrive()
    }
   });
}


//Google Drive API
function listFolderInGoogleDrive() {
    console.log('listFolderInGoogleDrive() was called')
    drive.files.list({
        auth: jwtClient,
        pageSize: 10,
        fields: 'nextPageToken, files(id, name)',
      }, (err, res) => {
        if (err) return console.log('The API returned an error: ' + err);
        console.log(JSON.stringify(res.data))
        console.log(JSON.stringify(res.data.files))
        const files = res.data.files;
        if (files.length) {
          console.log('Files:');
          files.map((file) => {
            console.log(`${file.name} (${file.id})`);
          });
        } else {
          console.log('No files found.');
        }
      });
    }

    function  CreateFolderInGoogleDrive() {
        console.log('CreateFolderInGoogleDrive() was called')
        var fileMetadata = {
            auth: jwtClient,
            'name': 'OfficService',
            'mimeType': 'application/vnd.google-apps.folder',
            'parents':['12CCq1GGoTyDW_Ox09TZf5BDgaPAjB0AR']
          };
          drive.files.create({
            resource: fileMetadata,
            fields: 'id'
          },(err, file)=> {
            if (err) {
              // Handle error
              console.error(err);
            } else {
             console.log('***parent****'+ file.data.id) 
           
            }
          });
        }

注意:驱动器文件夹“12CCq1GGoTyDW_Ox09TZf5BDgaPAjB0AR”已与服务帐户电子邮件 ID 共享。

以下是反映失败位置的控制台日志的一些结果。

token******{"access_token":"**********","token_type":"Bearer","expiry_date":1593865140000,"refresh_token":"jwt-placeholder"}

调用了 listFolderInGoogleDrive()

调用了 CreateFolderInGoogleDrive()

错误:需要登录

我想,我没有使用令牌进行身份验证。请帮助我 Node.js 代码以使用此令牌对服务帐户进行身份验证。

我要修改如下修改

修改点:

  • exports.handler = async (req, res) => {}处,最后的}没有使用。所以你的脚本不完整。请注意这一点。
    • 根据你的问题,我认为这可能是脚本的错误副本。
  • 在您的脚本中,jwtClient 未在 listFolderInGoogleDrive()CreateFolderInGoogleDrive() 处使用。我认为这可能是您遇到问题的原因。
  • CreateFolderInGoogleDrive(),在{resource: fileMetadata, fields: "id"}的对象中需要使用auth: jwtClient
    • 但在这种情况下,我想建议将 jwtClient 包含在 drive 中,例如 google.drive({ version: "v3", auth: jwtClient })

当以上几点反映到你的脚本中,就会变成如下。在这种情况下,修改了authWithServicePrivateKeylistFolderInGoogleDriveCreateFolderInGoogleDrive的功能。

修改后的脚本:

function authWithServicePrivateKey(jwtClient) {
  // Modified
  jwtClient.authorize(function (err) {
    if (err) {
      console.log("Could not connect***!" + err);
      return;
    }
  });
  drive = google.drive({ version: "v3", auth: jwtClient }); // Added
  listFolderInGoogleDrive();
  CreateFolderInGoogleDrive();
}

//Google Drive API
function listFolderInGoogleDrive() {
  console.log("listFolderInGoogleDrive() was called");
  drive.files.list(
    {
      //   auth: jwtClient,  // Removed
      pageSize: 10,
      fields: "nextPageToken, files(id, name)",
    },
    (err, res) => {
      if (err) return console.log("The API returned an error: " + err);
      console.log(JSON.stringify(res.data));
      console.log(JSON.stringify(res.data.files));
      const files = res.data.files;
      if (files.length) {
        console.log("Files:");
        files.map((file) => {
          console.log(`${file.name} (${file.id})`);
        });
      } else {
        console.log("No files found.");
      }
    }
  );
}

function CreateFolderInGoogleDrive() {
  console.log("CreateFolderInGoogleDrive() was called");
  var fileMetadata = {
    // auth: jwtClient,  // Removed
    name: "OfficService",
    mimeType: "application/vnd.google-apps.folder",
    parents: ['12CCq1GGoTyDW_Ox09TZf5BDgaPAjB0AR'],
  };
  drive.files.create(
    {
      resource: fileMetadata,
      fields: "id",
    },
    (err, file) => {
      if (err) {
        // Handle error
        console.error(err);
      } else {
        console.log("***parent****" + file.data.id);
      }
    }
  );
}

注:

  • 在此修改中,假设您已经能够使用您的服务帐户使用 Drive API 获取和放置 Google Drive 的值。请注意这一点。

已添加:

当您要检索您手动创建的文件的文件列表时,请按如下方式修改范围。

发件人:

['https://www.googleapis.com/auth/drive.file'],

收件人:

['https://www.googleapis.com/auth/drive'],
  • 关于https://www.googleapis.com/auth/drive.file的范围,the official document表示如下。

    Per-file access to files created or opened by the app. File authorization is granted on a per-user basis and is revoked when the user deauthorizes the app.

另外,您只想获取12CCq1GGoTyDW_Ox09TZf5BDgaPAjB0AR的共享文件夹的文件列表,请修改如下。

发件人:

drive.files.list({
    auth: jwtClient,
    pageSize: 10,
    fields: 'nextPageToken, files(id, name)',
  }, (err, res) => {

收件人:

drive.files.list({
    auth: jwtClient,
    pageSize: 10,
    fields: 'nextPageToken, files(id, name)',
    q: "'12CCq1GGoTyDW_Ox09TZf5BDgaPAjB0AR' in parents",  // Added
  }, (err, res) => {