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

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

我正在尝试使用c#通过休息http请求使用Google Cloud Vision。如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 PERMISSION DENIED代码:

Cloud Vision API之前尚未在项目XXXXX中使用或被禁用。通过访问https://console.developers.google.com/apis/api/vision.googleapis.com/overview?project=XXXXXXX启用它,然后重试。如果您最近启用了此API,请等待几分钟,以便将操作传播到我们的系统并重试。

我当然检查过,API已激活,启用并且没有API限制:API Key: No restrictions

由于似乎存在使用该认证方法的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}

我错过了什么?

c# rest authentication google-cloud-platform google-cloud-vision
1个回答
0
投票

原来它不足以激活API密钥(即使它没有任何限制),但需要主动转到Google console菜单 - > API和服务 - >库 - >搜索“Cloud Vision” - > klick on Cloud vision API并激活它。这需要几分钟。仪表板中未显示此状态。

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

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