我如何使用Google Cloud Platform REST API中的服务帐户进行身份验证

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

我想使用服务帐户密钥对Google Cloud Platform进行REST API调用,但是我无法弄清楚如何对该调用进行身份验证。通常,我将使用提供的客户端库并将密钥传递给它,但是在这种情况下,客户端库不支持该特定端点。

这是我要呼叫的其余端点:https://cloud.google.com/datastore/docs/reference/admin/rest/v1/projects/export

我需要从nodejs应用程序进行调用,所以这是相应的客户端库:https://github.com/googleapis/nodejs-datastore

The documentation专注于一般概念,但是我找不到关于如何使用正确的凭据实际进行REST调用的具体方法。

google-cloud-platform google-cloud-datastore
1个回答
1
投票

[我发现Google有一个用于nodejs的google-auth-library,它在内部处理OAuth流。我能够使用它成功进行身份验证的呼叫。

这是代码:

const auth = await new GoogleAuth({
    keyFilename: pathToKey,
    scopes: 'https://www.googleapis.com/auth/cloud-platform',
}).getClient()

const result = await auth.request({
    url: `https://datastore.googleapis.com/v1/projects/${projectId}:export`,
    method: 'POST',
    body: JSON.stringify(payload),
})

注意,这处理了身份验证部分。我仍然必须授予服务帐户正确的特权才能使授权部分正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.