node.js - Google服务帐户: The API returned an error: TypeError: source. hasOwnProperty在一小时后不再是一个函数

标签 node.js amazon-web-services express google-cloud-platform service-accounts

我已经在项目中添加了谷歌云服务帐户及其工作。但问题是,一个小时后(我认为),我收到此错误:

The API returned an error: TypeError: source.hasOwnProperty is not a function
Internal Server Error

我需要重新启动应用程序才能使其正常工作。

在这里StackOverflow post ,我发现了这个:

Once you get an access token it is treated in the same way - and is expected to expire after 1 hour, at which time a new access token will need to be requested, which for a service account means creating and signing a new assertion.

但没有帮助。 我正在使用 Node js 和亚马逊 secret 服务:

我用来授权的代码:


        const jwtClient = new google.auth.JWT(
            client_email,
            null,
            private_key,
            scopes
        );

        jwtClient.authorize((authErr) =>{
            if(authErr){
                const deferred = q.defer();
                deferred.reject(new Error('Google drive authentication error, !'));
            }
        });

有什么想法吗?

提示:AWS key 中是否有任何访问 key 的策略或谷歌云中是否有访问服务帐户的策略?例如本地访问还是在线访问?

最佳答案

[注意:您正在使用服务帐户访问 Google 云端硬盘。服务帐户将拥有自己的 Google 云端硬盘。与服务帐户共享您的 Google 云端硬盘是您的意图还是目标?]

Is there any policy in AWS secret to access a secret or in google cloud to access a service account? for example access in local or online?

我不知道你在问什么。 AWS 有 IAM 策略来控制 secret 管理。由于您可以从存储的 secret 创建签名 JWT,因此我认为这不是问题。 Google 没有关于访问服务帐户的政策 - 如果您拥有服务帐户 JSON key Material ,您可以执行服务帐户被授权执行的任何操作,直到服务帐户被删除、修改等。

现在讨论真正的问题。

您的签名 JWT 已过期,您需要创建一个新的。您需要跟踪您创建的 token 的生命周期,并在 token 过期之前重新创建/刷新 token 。 Google 世界中的默认过期时间是 3,600 秒。由于您正在创建自己的 token ,因此您的 token 周围没有“包装”代码来处理过期问题。

您收到的错误是由代码崩溃引起的。由于您没有包含代码,因此我无法告诉您在哪里。但是,解决方案是捕获错误,以便可以管理过期异常。

我建议您使用服务帐户创建客户端,而不是使用签名 JWT 创建 Google Drive 客户端。 token 过期和刷新将为您管理。

很少有 Google 服务仍然支持签名 JWT(您的代码正在使用它)。您应该改用服务帐户,该帐户从签名 JWT 开始,然后在内部将其交换为 OAuth 2.0 访问 token 。

有多个库可供您使用。以下任一功能都将提供您应该使用的功能,而不是制作您自己的签名 JWT。

https://github.com/googleapis/google-auth-library-nodejs

https://github.com/googleapis/google-api-nodejs-client

以下代码是一个“示例”,并不用于测试和调试。更改本示例中的范围以满足您的需求。删除我加载 service-account.json 文件的部分并替换为您的 AWS Secrets 代码。使用您所需的功能填写代码。如果您遇到问题,请使用您编写的代码和详细的错误消息创建一个新问题。

const {GoogleAuth} = require('google-auth-library');
const {google} = require('googleapis');

const key = require('service-account.json');

/**
 * Instead of specifying the type of client you'd like to use (JWT, OAuth2, etc)
 * this library will automatically choose the right client based on the environment.
 */
async function main() {
  const auth = new GoogleAuth({
    credentials: {
      client_email: key.client_email,
      private_key: key.private_key,
    },
    scopes: 'https://www.googleapis.com/auth/drive.metadata.readonly'
  });

  const drive = google.drive('v3');

  // List Drive files.
  drive.files.list({ auth: auth }, (listErr, resp) => {
    if (listErr) {
      console.log(listErr);
      return;
    }
    resp.data.files.forEach((file) => {
      console.log(`${file.name} (${file.mimeType})`);
    });
  });
}

main()

关于node.js - Google服务帐户: The API returned an error: TypeError: source. hasOwnProperty在一小时后不再是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57439342/

相关文章:

node.js - Mongoose - 由::11000 E11000 重复键错误索引引起?

javascript - 修改 s3.putObject 函数内的变量

node.js - 在 NodeJS 中,是否可以在子目录上运行 Node 服务器?

javascript - 文件系统 readFile 和 xml2js parseString 的待处理请求

javascript - 修改req.body和res。 Node.Js/Express 应用程序中的参数

amazon-web-services - 如何加快 AWS Fargate 上的部署?

amazon-web-services - 如何使用与现有分发版相同的 CNAME 测试 Cloudfront 分发版?

python - 使用JQ从CloudFormation.template.json中删除 block

node.js - 通过 Node js 测试 Backbone 模型

node.js - GraphQL-js Node/Express : How to pass a stringed object in a GraphQL Query?