1

我正在尝试按照 api.ai 文档中的建议使用 POST /entity 请求创建实体。你能告诉我为什么实体没有生成并得到异常为 ai.api.AIServiceException: Uri is not found or some resource with provided id is not found。

我得到的回应是:

 {
    "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 附加在 post 请求正文中:

  [{
  "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 代码片段:

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();
4

1 回答 1

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);
于 2017-02-27T11:36:53.100 回答