我正在尝试使用api.ai文档中建议的POST /实体请求创建实体。 你能告诉我为什么实体没有生成并获得异常 ai.api.AIServiceException:找不到Uri或找不到提供id的某些资源。
我得到了回复:
{
"id": "d2c20de1-4a65-4b3e-8a28-48f1be009f1a",
"timestamp": "2016-11-03T11:26:18.871Z",
"status": {
"code": 404,
"errorType": "not_found",
"errorDetails": "Entity 'Appliances' is not found.",
"errorID": "12dadf38-2579-41a9-abd5-24572e64c1e4"
}
}
邮政请求正文中附带的Postdata JSON:
[{
"name": "Appliances",
"entries": [
{
"value": "Coffee Maker",
"synonyms": [
"coffee maker",
"coffee machine",
"coffee"
]
},
{
"value": "Thermostat",
"synonyms": [
"Thermostat",
"heat",
"air conditioning"
]
},
{
"value": "Lights",
"synonyms": [
"Lights",
"Light",
"lamps"
]
}
]
}
]
Java Code Snippet:
HttpURLConnection connection = null;
final URL url = new URL(endpoint);
final String postJsonData = requestJson;
Log.debug("Request json: " + postJsonData);
System.out.println(postJsonData);
if (config.getProxy() != null) {
connection = (HttpURLConnection) url.openConnection(config.getProxy());
} else {
connection = (HttpURLConnection) url.openConnection();
}
//post request
connection.setRequestMethod("POST");
connection.addRequestProperty("Authorization", "Bearer " + config.getApiKey());
connection.addRequestProperty("Content-Type", "application/json; charset=utf-8");
connection.addRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.connect();
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(postJsonData);
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String output;
StringBuffer response = new StringBuffer();
while ((output = in.readLine()) != null) {
response.append(output);
}
in.close();
答案 0 :(得分:0)
使用开发人员访问令牌,而不是使用客户端访问令牌。 您可以使用以下代码:
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://api.api.ai/v1/entities?v=20150910");
StringEntity params =new StringEntity("{\"name\":\"Appliances\",\"entries\":[{\"value\":\"Coffee Maker\",\"synonyms\":[\"coffee maker\",\"coffee machine\",\"coffee\"]},{\"value\":\"Thermostat\",\"synonyms\":[\"Thermostat\",\"heat\",\"air conditioning\"]},{\"value\":\"Lights\",\"synonyms\":[\"lights\",\"light\",\"lamps\"]},{\"value\":\"Garage door\",\"synonyms\":[\"garage door\",\"garage\"]}]}");
request.addHeader("content-type", "application/json; charset=utf-8");
request.addHeader("Authorization", "Bearer 92f6b9908d8c4140a71d20059c2b773b");
request.addHeader("Accept", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);