javaandroidrestapizimbra

Consume Zimbra Get Calendar REST Api in Java


I get this error when I consume the REST Api of Zimbra on Android: must Athenticate java.lang.RuntimeException: Failed : HTTP error code : 401 at ZimbraREST.main(ZimbraREST.java:33). However I am absolutely sure that my login and my password are the good ones and my code was working perfectly fine yesterday, and I did not modify any important stuff linked to this code. This code should get me an xml file that I use to get sync calendar on my app. Thanks for your help. Here is my code:

@Override
protected Void doInBackground(Integer... integers) {
    System.out.println("15email_adress=" + email_adress);
    System.out.println("15password=" + password);
    String dayStart = String.valueOf(integers[0]);
    String monthStart = String.valueOf(integers[1]);
    String yearStart = String.valueOf(integers[2]);
    String dayEnd = String.valueOf(integers[3]);
    String monthEnd = String.valueOf(integers[4]);
    String yearEnd = String.valueOf(integers[5]);
    String[] strings = {dayStart, monthStart, yearStart, dayEnd, monthEnd, yearEnd};

    int k = 0;
    for(String s: strings){
        if(s.length() < 2){
            strings[k] = "0" + s;
        }
        k++;
    }

    dayStart = strings[0];
    monthStart = strings[1];
    yearStart = strings[2];
    dayEnd = strings[3];
    monthEnd = strings[4];
    yearEnd = strings[5];

    try {
        URL url = new URL("https://zmail.insa-lyon.fr/home/" + email_adress + "/Calendrier%20Cocktail?fmt=xml&start=" +
                monthStart +
                "/" +
                dayStart +
                "/" +
                yearStart +
                "&end=" +
                monthEnd +
                "/" +
                dayEnd +
                "/" +
                yearEnd);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(email_adress, password.toCharArray());
            }
        });
        conn.setRequestMethod("GET");
        conn.setRequestProperty("--user", email_adress + ":" + password);
        if (conn.getResponseCode() != 200) {
            System.out.println("15erreur=" + url.toString());
            System.out.println("15erreur=" + conn.getResponseMessage());
            System.out.println("15erreur=" + conn.getRequestMethod());
            return null;
        }
        System.out.println(conn.getResponseMessage());
        InputStreamReader inputStreamReader = new InputStreamReader((conn.getInputStream()));
        BufferedReader br = new BufferedReader(inputStreamReader);
        String output;

        StringBuilder xml = new StringBuilder();
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            xml.append(output);
        }
        inputStreamReader.close();
        br.close();
        conn.disconnect();
        System.out.println(xml);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new InputSource(new StringReader(xml.toString())));
        rootElement = document.getDocumentElement();
    } catch (IOException | ParserConfigurationException | SAXException e) {
        e.printStackTrace();
    }
    return null;
}

Solution

  • Ok so I had to authenticate the user with the code below:

            // Authentication part -------------------
            String userpass = email_adress + ":" + password;
            conn.setRequestProperty("X-Requested-With", "Curl");
            String basicAuth = "Basic " + new String(android.util.Base64.encode(userpass.getBytes(), android.util.Base64.DEFAULT));
            conn.setRequestProperty("Authorization", basicAuth);
            // ----------------------------------------