Google service account: The API returned an error: TypeError: source.hasOwnProperty is not a function after an hour

Google service account: The API returned an error: TypeError: source.hasOwnProperty is not a function after an hour

我在一个项目中添加了 google 云服务帐户并开始工作。但问题是,一个小时后(我想),我得到这个错误:

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

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

这里Whosebug 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 和亚马逊秘密服务:

我授权的代码:


        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 secret 中是否有访问 secret 或在 google cloud 中访问服务帐户的策略?例如在本地或在线访问?

[注意:您正在使用服务帐户访问 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 策略来控制秘密管理。由于您能够从存储的秘密创建签名的 JWT,我认为这不是问题。 Google 没有关于访问服务帐户的策略 - 如果您有服务帐户 JSON 密钥 material,您可以执行服务帐户被授权执行的任何操作,直到服务帐户被删除,修改等

现在进入真正的问题。

您的 Signed JWT 已过期,您需要创建一个新的。您需要跟踪您创建的令牌的生命周期和 recreate/refresh 令牌在它们过期之前。 Google 世界中的默认过期时间是 3,600 秒。由于您正在创建自己的令牌,因此您的令牌周围没有 "wrapper" 代码来处理过期。

您遇到的错误是由代码崩溃引起的。因为你没有包含你的代码,所以我不能告诉你在哪里。但是,解决方案是捕获错误以便管理过期异常。

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

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

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

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

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

以下代码是 "example" 并且不打算进行测试和调试。更改此示例中的 scopes 以匹配您的要求。删除我加载 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()