为什么我对 Kairos api 的 REST 请求说请求缺少必需的参数?
Why does my REST request to Kairos api say that the request is missing a required parameter?
我正在尝试连接到 kairos api 以熟悉它并使用它们的检测功能。
尚无此 api 的官方 C# 库。有一个似乎没有得到积极维护。
我正在尝试使用 RestSharp 直接连接。如果我遗漏 app_id 和用于身份验证的应用程序密钥,服务器正在接收我的请求并像我期望的那样响应。所以那部分(添加 headers)似乎有效。
添加参数似乎失败了。根据他们的网站:https://www.kairos.com/docs/face-recognition/
唯一需要的参数是 "image" 和 url 或 base64 编码的照片。
我添加了参数:
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");
但响应仍然是:"Error code: 1002",
"Message": One or more required parameters are missing.
我怀疑 uri 的构造方式存在问题,但我真的无法确定。我复制了语法
request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");
来自我之前提到的同一个 C# SDK。我也简单地尝试过:
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", imageURL);
没有成功。
谁能帮帮我?
编辑:
需要说明的是,完整代码如下所示:
static void Main(string[] args)
{
var client = new RestClient();
client.BaseUrl = new Uri("https://api.kairos.com/");
var request = new RestRequest("detect", Method.POST);
request.AddHeader("Content-Type", "application/JSON");
request.AddHeader("app_id", "MY app ID");
request.AddHeader("app_key", "My app KEY");
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", "\"url\":\"" + imageURL + "\"");
request.AddParameter("selector:", "FACE");
request.AddParameter("minHeadScale:", "0.125");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
您必须在发送之前将您的参数序列化为 JSON。
我创建了一个 dotnetfiddle 来演示:
https://dotnetfiddle.net/VrKZo2
文档并没有错,您可以只传递 image
字段,一切正常。
namespace ConsoleApplication3
{
using System;
using RestSharp;
public class Program
{
public static void Main()
{
string appId = "YOUR APP ID HERE";
string appKey = "YOUR APP KEY HERE";
var client = new RestClient("https://api.kairos.com");
var request = new RestRequest("detect", Method.POST);
// automatically makes the request body serialize as JSON
request.RequestFormat = DataFormat.Json;
request.AddBody(new { image = "http://media.kairos.com/kairos-elizabeth.jpg" });
request.AddHeader("app_id", appId);
request.AddHeader("app_key", appKey);
var response = client.Execute(request);
// handle response however you want, but I'm just going to print it out
Console.WriteLine(response.Content);
}
}
}
当我 运行 设置了 appId
和 appKey
时,我得到了这个内容:
{
"images": [
{
"time": 0.19603,
"status": "Complete",
"file": "face_55b7d7c4d2008.jpg",
"width": 1536,
"height": 2048,
"faces": [
{
"topLeftX": 300,
"topLeftY": 526,
"width": 934,
"height": 934,
"leftEyeCenterX": -1,
"leftEyeCenterY": -1,
"rightEyeCenterX": -1,
"rightEyeCenterY": -1,
"noseTipX": -1,
"noseTipY": -1,
"noseBtwEyesX": -1,
"noseBtwEyesY": -1,
"chinTipX": -1,
"chinTipY": -1,
"leftEyeCornerLeftX": -1,
"leftEyeCornerLeftY": -1,
"leftEyeCornerRightX": -1,
"leftEyeCornerRightY": -1,
"rightEyeCornerLeftX": -1,
"rightEyeCornerLeftY": -1,
"rightEyeCornerRightX": -1,
"rightEyeCornerRightY": -1,
"rightEarTragusX": -1,
"rightEarTragusY": -1,
"leftEarTragusX": -1,
"leftEarTragusY": -1,
"leftEyeBrowLeftX": -1,
"leftEyeBrowLeftY": -1,
"leftEyeBrowMiddleX": -1,
"leftEyeBrowMiddleY": -1,
"leftEyeBrowRightX": -1,
"leftEyeBrowRightY": -1,
"rightEyeBrowLeftX": -1,
"rightEyeBrowLeftY": -1,
"rightEyeBrowMiddleX": -1,
"rightEyeBrowMiddleY": -1,
"rightEyeBrowRightX": -1,
"rightEyeBrowRightY": -1,
"nostrilLeftHoleBottomX": -1,
"nostrilLeftHoleBottomY": -1,
"nostrilRightHoleBottomX": -1,
"nostrilRightHoleBottomY": -1,
"nostrilLeftSideX": -1,
"nostrilLeftSideY": -1,
"nostrilRightSideX": -1,
"nostrilRightSideY": -1,
"lipCornerLeftX": -1,
"lipCornerLeftY": -1,
"lipLineMiddleX": -1,
"lipLineMiddleY": -1,
"lipCornerRightX": -1,
"lipCornerRightY": -1,
"pitch": -1,
"yaw": -1,
"roll": -1
}
]
}
]
}
我正在尝试连接到 kairos api 以熟悉它并使用它们的检测功能。
尚无此 api 的官方 C# 库。有一个似乎没有得到积极维护。
我正在尝试使用 RestSharp 直接连接。如果我遗漏 app_id 和用于身份验证的应用程序密钥,服务器正在接收我的请求并像我期望的那样响应。所以那部分(添加 headers)似乎有效。
添加参数似乎失败了。根据他们的网站:https://www.kairos.com/docs/face-recognition/
唯一需要的参数是 "image" 和 url 或 base64 编码的照片。
我添加了参数:
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");
但响应仍然是:"Error code: 1002",
"Message": One or more required parameters are missing.
我怀疑 uri 的构造方式存在问题,但我真的无法确定。我复制了语法
request.AddParameter("image", "{\"url\":\"" + imageURL + "\"}");
来自我之前提到的同一个 C# SDK。我也简单地尝试过:
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", imageURL);
没有成功。
谁能帮帮我?
编辑: 需要说明的是,完整代码如下所示:
static void Main(string[] args)
{
var client = new RestClient();
client.BaseUrl = new Uri("https://api.kairos.com/");
var request = new RestRequest("detect", Method.POST);
request.AddHeader("Content-Type", "application/JSON");
request.AddHeader("app_id", "MY app ID");
request.AddHeader("app_key", "My app KEY");
var imageURL = new Uri("http://media.kairos.com/kairos-elizabeth.jpg");
request.AddParameter("image", "\"url\":\"" + imageURL + "\"");
request.AddParameter("selector:", "FACE");
request.AddParameter("minHeadScale:", "0.125");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
}
您必须在发送之前将您的参数序列化为 JSON。
我创建了一个 dotnetfiddle 来演示: https://dotnetfiddle.net/VrKZo2
文档并没有错,您可以只传递 image
字段,一切正常。
namespace ConsoleApplication3
{
using System;
using RestSharp;
public class Program
{
public static void Main()
{
string appId = "YOUR APP ID HERE";
string appKey = "YOUR APP KEY HERE";
var client = new RestClient("https://api.kairos.com");
var request = new RestRequest("detect", Method.POST);
// automatically makes the request body serialize as JSON
request.RequestFormat = DataFormat.Json;
request.AddBody(new { image = "http://media.kairos.com/kairos-elizabeth.jpg" });
request.AddHeader("app_id", appId);
request.AddHeader("app_key", appKey);
var response = client.Execute(request);
// handle response however you want, but I'm just going to print it out
Console.WriteLine(response.Content);
}
}
}
当我 运行 设置了 appId
和 appKey
时,我得到了这个内容:
{
"images": [
{
"time": 0.19603,
"status": "Complete",
"file": "face_55b7d7c4d2008.jpg",
"width": 1536,
"height": 2048,
"faces": [
{
"topLeftX": 300,
"topLeftY": 526,
"width": 934,
"height": 934,
"leftEyeCenterX": -1,
"leftEyeCenterY": -1,
"rightEyeCenterX": -1,
"rightEyeCenterY": -1,
"noseTipX": -1,
"noseTipY": -1,
"noseBtwEyesX": -1,
"noseBtwEyesY": -1,
"chinTipX": -1,
"chinTipY": -1,
"leftEyeCornerLeftX": -1,
"leftEyeCornerLeftY": -1,
"leftEyeCornerRightX": -1,
"leftEyeCornerRightY": -1,
"rightEyeCornerLeftX": -1,
"rightEyeCornerLeftY": -1,
"rightEyeCornerRightX": -1,
"rightEyeCornerRightY": -1,
"rightEarTragusX": -1,
"rightEarTragusY": -1,
"leftEarTragusX": -1,
"leftEarTragusY": -1,
"leftEyeBrowLeftX": -1,
"leftEyeBrowLeftY": -1,
"leftEyeBrowMiddleX": -1,
"leftEyeBrowMiddleY": -1,
"leftEyeBrowRightX": -1,
"leftEyeBrowRightY": -1,
"rightEyeBrowLeftX": -1,
"rightEyeBrowLeftY": -1,
"rightEyeBrowMiddleX": -1,
"rightEyeBrowMiddleY": -1,
"rightEyeBrowRightX": -1,
"rightEyeBrowRightY": -1,
"nostrilLeftHoleBottomX": -1,
"nostrilLeftHoleBottomY": -1,
"nostrilRightHoleBottomX": -1,
"nostrilRightHoleBottomY": -1,
"nostrilLeftSideX": -1,
"nostrilLeftSideY": -1,
"nostrilRightSideX": -1,
"nostrilRightSideY": -1,
"lipCornerLeftX": -1,
"lipCornerLeftY": -1,
"lipLineMiddleX": -1,
"lipLineMiddleY": -1,
"lipCornerRightX": -1,
"lipCornerRightY": -1,
"pitch": -1,
"yaw": -1,
"roll": -1
}
]
}
]
}