gpt4 book ai didi

go - 如何使用 Google Cloud 客户端库进行基于 API key 的 REST 调用

转载 作者:IT王子 更新时间:2023-10-29 02:32:04 24 4
gpt4 key购买 nike

我正在尝试使用来自 Golang 的 API key 对 Google Cloud Vision API 进行 API 调用。但我收到了 400:错误请求,invalid_grant 错误

下面代码的 apiKey/apiKeyOption 部分是我的。

调用电话的正确方式是什么?有可能吗?

import (
// ...
"google.golang.org/api/option"
vision "cloud.google.com/go/vision/apiv1"
"golang.org/x/net/context"
)

func getImageLabels(filename string) []string {

ctx := context.Background()

apiKey := "..." // I have a valid api key generated in the console.
apiKeyOption := option.WithAPIKey(apiKey)

client, err := vision.NewImageAnnotatorClient(ctx, apiKeyOption)

// ...
labels, err := client.DetectLabels(ctx, image, nil, 10)

}

...

Failed to detect labels: rpc error: code = Internal desc = transport: oauth2: cannot fetch token: 400 Bad Request
Response: {
"error" : "invalid_grant"
}

最佳答案

根据我在 Golang 的 Google Cloud 客户端库的官方文档(GitHubGolang documentation )中找到的内容,当前支持的身份验证方法是 Google Application Default Credentials服务帐户,因此没有使用 API key 进行身份验证的选项。

然而,正如指出的in the documentation ,出于安全原因,Google 建议使用服务帐户而不是 API key ,因此我建议您尽可能使用该选项。

API keys can be used when calling Google APIs that don't require authentication, and when using Google Cloud Endpoints. For security reasons, we recommend using service accounts instead.

关于go - 如何使用 Google Cloud 客户端库进行基于 API key 的 REST 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com