Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic authentication to access assembla rest apis from android

Tags:

android

I want to use assembla apis from android environment for my project. I am trying to do basic authentication as follow :

String authentication = "username:password";
String encoding = Base64.encodeToString(authentication.getBytes(), 0);    

     URL url = new URL("https://www.assembla.com/");

        conn = (HttpURLConnection) url.openConnection();

        conn.setRequestMethod("GET");

        conn.setRequestProperty("Authorization", "Basic " + encoding);
        conn.setDoOutput(true);

        conn.connect();
        System.out.println(conn.getResponseCode());

        System.out.println(conn.getResponseMessage());

I am getting 400 and Bad Request in output. is there something wrong with URL that i am using or some other thing is going wrong?

like image 899
Nisha Jain Avatar asked Aug 18 '11 10:08

Nisha Jain


1 Answers

It looks like the question was answered here. You need to use Base64.NO_WRAP flag when encoding username-password pair:

String encoding = Base64.encodeToString(authentication.getBytes(), Base64.NO_WRAP);

By default the Android Base64 util adds a newline character to the end of the encoded string. This invalidates the HTTP headers and causes the "Bad request".

The Base64.NO_WRAP flag tells the util to create the encoded string without the newline character thus keeping the HTTP headers intact.

like image 81
Konstantin Burov Avatar answered Oct 01 '22 01:10

Konstantin Burov