我在 .NET Web 应用程序中实现自定义 AutoML 模型时遇到问题,该模型允许通过 REST API 发送图像以被识别。我不断收到错误。
远程服务器返回错误:(403) Forbidden。
我有一个图像并将其转换为一个称为字节的字符串,imageBytesString
并创建了 jsonRequest 对象,如下所示:
string jsonRequest = "{\"payload\":{\"image\":{\"imageBytes\":\"" + imageBytesString + "\"},}}";`
然后我正在执行如下的 POST 请求:
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();
如果没有其他信息给我上述错误。
作为参考,这些是从我的自定义 AutoML 模型的 PREDICT 页面底部截取的片段:
请求.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
谢谢