“((403)Forbidden)"尝试通过REST API将图像发送到我的自定义AutoML模型时 [英] "(403) Forbidden" when trying to send an image to my custom AutoML model via the REST API

查看:118
本文介绍了“((403)Forbidden)"尝试通过REST API将图像发送到我的自定义AutoML模型时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET Web应用程序中实现自定义AutoML模型时遇到问题,该模型允许识别通过REST API发送图像.我不断收到错误消息.

I am having issues with implementing a custom AutoML model in a .NET web app that allows sending images via the REST API to be recognized. I keep getting errors.

远程服务器返回错误:(403)禁止.

The remote server returned an error: (403) Forbidden.

我有一张图片,并将其转换为一个名为imageBytesString的字节串,并创建了jsonRequest对象,如下所示:

I have an image and converted it into a string of bytes called imageBytesString and have created the jsonRequest object like this:

string jsonRequest = "{\"payload\":{\"image\":{\"imageBytes\":\"" + imageBytesString + "\"},}}";`

然后我正在执行POST请求,如下所示:

Then I'm doing a POST request like follows:

WebRequest request = WebRequest.Create(@"https://automl.googleapis.com/v1beta1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict");
request.Method = "POST";
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer GCLOUD_ACCESS_TOKEN");

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    streamWriter.Write(jsonRequest);
}

然后,当它命中request.GetResponse();时,如果没有其他信息就给了我以上错误.

Then when it hits request.GetResponse(); if gives me the above error with no other information.

作为参考,以下是摘自我的自定义AutoML模型的PREDICT页面底部的摘录:

For reference, these are the snippets taken from the bottom of the PREDICT page on my custom AutoML model:

request.json:

{
  "payload": {
    "image": {
      "imageBytes": "YOUR_IMAGE_BYTE"
    },
  }
}

执行请求:

curl -X POST -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
  https://automl.googleapis.com/v1beta1/projects/PROJECT_ID/locations/us-central1/models/MODEL_ID:predict -d @request.json

谢谢

推荐答案

我终于能够使它正常工作.

I was finally able to get it working.

要解决此问题,必须解决2个问题.

There were 2 issues that needed to be fixed for it to work.

首先;由于某种原因,当我最初创建服务帐户并输入命令gcloud auth activate-service-account --key-file="[/PATH/TO/KEY/FILE.json]时,它告诉我它可以工作,并且该服务帐户已添加到gcloud auth list列表中.但是在再次检查时,它不存在,所以我再次运行它.

Firstly; for some reason, when I initially created the service account and entered the command gcloud auth activate-service-account --key-file="[/PATH/TO/KEY/FILE.json], it told me that it worked and the service account was added to the gcloud auth list list. But upon checking again, it wasn't there so I ran it again.

第二;我通过以下命令获取访问令牌:

Secondly; I was getting the access token with the following command:

gcloud auth application-default print-access-token

相反,我尝试使用通过以下命令收到的访问令牌:

Instead, I tried using the access token received with the following command:

gcloud auth print-access-token

(没有application-default)在其中.

完成上述两项操作后,我现在能够成功发送图像请求并接收预测响应.

After I had done both those things, I am now able to successfully send an image request and receive the prediction response.

但是,尽管如此,我似乎还是每小时仍需要获取一个新的访问令牌.但这是我想的另一个问题.

But it seems I still have to get a new access token every hour though... But that's another issue I suppose.

这篇关于“((403)Forbidden)"尝试通过REST API将图像发送到我的自定义AutoML模型时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆