google-cloud-platformvideo-intelligence-api

Calling the rest video intelligence API in JAVA


I've been trying to call the rest API of video intelligence through a java program to annotate a local file. Here is my code:

        byte[] data = Files.readAllBytes(path);
        byte[] encodedBytes = Base64.encodeBase64(data);

        URIBuilder builder = new URIBuilder("https://videointelligence.googleapis.com/v1beta2/videos:annotate");
        URI uri = builder.build();
        HttpPost request = new HttpPost(uri);
        request.setHeader("Content-Type", "application/json");
        request.setHeader("X-Goog-Api-Key",MyKey);

        JSONObject json = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        jsonArray.put("LABEL_DETECTION");
        json.put("inputContent", encodedBytes);
        json.put("features", jsonArray);

        StringEntity reqEntity = new StringEntity(json.toString());
        request.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(request);
        HttpEntity entity = response.getEntity();

I'm getting this error: "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"input_content\": Proto field is not repeating, cannot start list.

Could anyone help me with this error please? thank you


Solution

  • The error message is not really helpful. Problem here is that "inputContent" should be String instead of byte[]. This worked for me:

    String str = new String(encodedBytes, "UTF-8");
    requestJson.put("inputContent", str);