简体   繁体   English

Node.js - 无法从 Google Drive API 访问我的帐户

[英]Node.js - Can't access my account from the Google Drive API

I need to programmatically modify my Google Drive, in terms of the ability to create folders and uploading to them a bunch of files, and then, when needed - remove that root folder and redo the whole process.我需要以编程方式修改我的 Google 云端硬盘,以便能够创建文件夹并将一堆文件上传到它们,然后在需要时删除该根文件夹并重做整个过程。

I've created a project that has a service account, then downloaded the JSON and it's stored on my computer.我创建了一个具有服务帐户的项目,然后下载了 JSON 并将其存储在我的计算机上。
Next, I followed this tutorial .接下来,我跟着这个教程

I ended up with this code:我最终得到了这个代码:

const auth = await google.auth.getClient({
  credentials: require(pathToServiceAccountJSON),
  scopes: "https://www.googleapis.com/auth/drive"
});

const drive = await google.drive({ version: "v3", auth });

drive.files
  .create({
    resource: {
      name: filename,
      mimeType: "application/vnd.google-apps.folder",
      parents: [parentId]
    }
  })
  .then(result => console.log("SUCCESS:", result))
  .catch(console.error);

However, executing it causing the following error to be thrown:但是,执行它会导致抛出以下错误:

{
  ...
  errors: [{
    domain: "global",
    reason: "forbidden",
    message: "Forbidden"
  }]
}

First off, if you get lost, this quick start from Google is probably better than the tutorial.首先,如果你迷路了,这个来自谷歌的快速入门可能比教程更好。

Secondly, to get access to your drive you must request the appropriate scope within your app, and you must authorize the app's requested permissions (scopes) by visiting a URL that will be provided during the authorization process.其次,要访问您的驱动器,您必须在您的应用程序中请求适当的范围,并且您必须通过访问将在授权过程中提供的 URL 来授权应用程序请求的权限(范围)。 Here is a guide to scopes . 这是范围指南

To be able to impersonate a user(like yourself or any other in a domain) with a service account you need to have it with domain-wide delegation on, to do this you need to have a G suite account [1].为了能够使用服务帐户模拟用户(如您自己或域中的任何其他人),您需要在域范围内启用它,为此您需要拥有一个 G Suite 帐户 [1]。 If this is the case, from the library example [2], you need to add the user you want to impersonate as the 5th parameter when constructing the JWT object:如果是这种情况,从库示例 [2] 中,您需要在构造 JWT 对象时将要模拟的用户添加为第 5 个参数:

const {JWT} = require('google-auth-library');
const keys = require('./jwt.keys.json');

async function main() {
  const client = new JWT(
    keys.client_email,
    null,
    keys.private_key,
    ['https://www.googleapis.com/auth/cloud-platform'],
    'userToImpersonate@example.com'
  );
  const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
  const res = await client.request({url});
  console.log(res.data);
}

If you don't have G Suite account, you could simply follow the quickstart [3] steps to get drive service and then make your create request with it.如果您没有 G Suite 帐户,您只需按照快速入门 [3] 步骤获取驱动器服务,然后使用它提出创建请求。

[1] Do I need G Suite account to make requests impersonating an user with a service account? [1] 是否需要 G Suite 帐户才能使用服务帐户模拟用户发出请求?

[2] https://github.com/googleapis/google-auth-library-nodejs#json-web-tokens [2] https://github.com/googleapis/google-auth-library-nodejs#json-web-tokens

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以设置我的 Node.js 后端访问我的 Google Drive 以列出/下载/上传吗? - Can I set my Node.js Backend to access my Google Drive to list/download/upload? 从 node.js google drive api 返回结果 - Return a result from node.js google drive api 无法使用 Node.js 验证谷歌服务帐户 - Can't authenticate google service account using Node.js 我可以通过 Node.js 使用服务帐户从 Google 驱动器下载图像的分步过程吗 - Can I get the step by step process for downloading images from Google drive using the service account via Node.js 使用Google Drive API和Node.js创建Google文档 - Create a Google Document with Google Drive API and Node.js 无法从 API 响应中提取 JSON - Google Cloud Vision API(Node.js 库) - Can't extract JSON from API response - Google Cloud Vision API (Node.js library) 我无法从外部访问我的AWS EC2上的node.js服务器 - I can't access my node.js server on my AWS EC2 isntance from the outside Node.js无法访问Google AdSense Management API。 '用户没有adsense帐户' - Node.js cannot access Google AdSense Management API. 'user does not have adsense account' Google Drive API下载NODE.JS示例不起作用 - Google Drive API download NODE.JS sample doesn't work Google Drive API混乱,如何下载文件夹内容? (的node.js) - Confusion on Google Drive API, how can I download contents of folders? (node.js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM