尝试使用 google-auth-library 进行身份验证时出现“emitWarning 不是函数”

问题描述 投票:0回答:1

我正在尝试让同一 VPC 中的两个 Cloud Run 服务相互通信。我正在关注本教程。我链接的部分是我遇到问题的部分。现在的错误代码如下所示:

    import { GoogleAuth } from 'google-auth-library';
    const auth = new GoogleAuth();
    
    client: any;
    clientHeaders: any;
    
    serviceUrl = "https://url"
    headers = {
        'Content-type': 'application/json; charset=UTF-8',
    };
    
    async setup() {
        try {
            if (!this.client)
                this.client = await auth.getIdTokenClient(this.serviceUrl); // error happens here
            const gcloudHeaders = await this.client.getRequestHeaders();
            this.headers = {
                ...gcloudHeaders,
                ...this.headers,
            };
            const res = await this.client.request({ url: this.serviceUrl });
            console.info(res.data);
        } catch (err) {
            throw Error(
                'could not create an identity token: ' + err.message
            );
        }
    }

此代码引发以下错误:

Uncaught (in promise) Error: could not create an identity token: o.emitWarning is not a function
at p.setup

查了一下,好像是nodeJS的问题,可能在

google-auth-library
里面。谷歌搜索这个问题并没有给我很多结果,而且没有一个特定于
google-auth-library
。版本:

nodeJS v20.10.0 
google-auth-library ^9.7.0

有什么想法吗?

node.js google-cloud-platform google-oauth google-api-nodejs-client google-auth-library
1个回答
0
投票

const res = await this.client.request({ url: this.serviceUrl });

在上面的行中,我没有看到您在哪里使用了应该包含授权(为您生成的令牌)的标头。

在您正在关注的示例中(使用

got
),您会注意到这一行

const serviceResponse = await got(serviceUrl, serviceRequestOptions);

在该示例中,他们对服务 URL 的调用包括

serviceRequestOptions
,其中包括他们通过调用
getRequestHeaders

检索到的标头
© www.soinside.com 2019 - 2024. All rights reserved.