如何通过 HTTP 请求验证 Google Cloud Vision

How to authenticate Google Cloud Vision via HTTP Request

我正在尝试使用 Google Cloud Vision 通过使用 c# 的 rest http 请求。如 here 所述,我尝试使用 api 密钥作为参数进行身份验证:

string uri = "https://vision.googleapis.com/v1/images:annotate?key=" + API_KEY;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
[...]
response = await client.SendAsync(request);

但是,我总是得到 403 权限被拒绝代码:

Cloud Vision API has not been used in project XXXXX before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=XXXXXXX then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.

当然查过了,API是激活的,启用的,没有API限制:

由于似乎存在 some problems 这种身份验证方法,尤其是云视觉,我尝试通过我创建的服务帐户的访问令牌进行身份验证。我授予服务帐户完全访问权限只是为了确保服务帐户的权限没有问题:

string uri = "https://vision.googleapis.com/v1/images:annotate";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri);
request.Headers.Add("Authorization", "Bearer " + ACCESS_TOKEN);
response = await client.SendAsync(request);

仍然是相同的错误消息。卷曲也是如此:

curl -k -X POST -H "Content-Type:application/json" 
-d "@{REQUEST_AS_JSON_OR_PATH_TO_JSON-FILE}" 
https://vision.googleapis.com/v1/images:annotate?key={API_KEY}

我错过了什么?

事实证明激活 API 键是不够的(即使它没有任何限制),但需要主动转到 Google console 菜单 -> API s and Services -> Library -> Search for "Cloud Vision" -> 点击 Cloud vision API 并激活它。这需要几分钟。此状态未显示在您的仪表板中。

然后,您可以简单地使用 api 密钥作为 URL 参数进行身份验证。