Tanveer
Tanveer

Reputation: 900

Unable to connect to omniture rest API 1.4

We are trying to connect to omniture rest API 1.4 using Java for report.Get. We are unable to create connection. The user id and password are working fine on UI but while making HTTP connection we are getting 400 bad request. Same code works fine with rest API 1.3 for company.reportSuites method. Code if failing while creating input stream. We did checked HTTP response code for connection at it is also coming 400.

public class OMTR_REST {
        private static String USERNAME = "XXXXXXX";
    private static String PASSWORD = "xXXXXXXXX";
    private static String ENDPOINT = "https://api.omniture.com/admin/1.4/rest/"; //san jose endpoint, change for your company's datacenter

    private OMTR_REST() {}

    public static String callMethod(String method, String data) throws IOException {
        URL url = new URL(ENDPOINT + "?method=" + method);
        URLConnection connection = url.openConnection();
        connection.addRequestProperty("X-WSSE", getHeader());

        connection.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
        wr.write(data);
        wr.flush();

        InputStream in = connection.getInputStream();
        BufferedReader res = new BufferedReader(new InputStreamReader(in, "UTF-8"));

        StringBuffer sBuffer = new StringBuffer();
        String inputLine;
        while ((inputLine = res.readLine()) != null)
            sBuffer.append(inputLine);

        res.close();

        return sBuffer.toString();

Upvotes: 0

Views: 2664

Answers (2)

rwsimmo
rwsimmo

Reputation: 1687

There was a change in the API from version 1.3 to 1.4. The Get method now returns a status of 400 if the report in not ready. To me, it was a bad choice to return a HTTP 400 error (Bad request) when the report in not ready but that is what they are doing. See page 13 in the document below.

https://github.com/AdobeDocs/analytics-1.4-apis

Upvotes: 1

freedev
freedev

Reputation: 30087

I see few mistakes in your sample:

  1. you should not use api.omniture.com for every request. First request should call api.omniture.com using Company.GetEndpoint method in order to get the correct endpoint, then use it for next requests.
  2. when a wrong endpoint is used you could receive an HTTP 301 response. I'm not sure your implementation handle this case.
  3. when you receive a HTTP 400 error (bad request). Well, that's exactly what has happened, in your example you're JSON writing directly into the body and many things could go wrong. Wrong type for a value, wrong upper/lower case for a key. Using a JAX-RS or another REST client should make your life simpler.

I have built a working Omniture REST API sample with JAX-RS where the model is clear easy to debug/modify.

Update

Recently I have found this:

https://github.com/Adobe-Marketing-Cloud/analytics-java-library

Upvotes: 0

Related Questions