user5236815 user5236815 - 1 month ago 10
Android Question

I get HTTP 400 when using oauth post request

I am trying to use oauth for an application i am writing for android. I am getting so far that a proper authorization code is returned to my application. Now Í need to make a post request with that to get the access/refresh token. To do that I have to following code:

class post_get_access_token extends AsyncTask<String, Void, String> {

private Exception exception;

protected String doInBackground(String... code) {
try {
String url = "https://login.eveonline.com/oauth/token";
String charset = "UTF-8";

String grant_type = "authorization_code";
String enc_auth = Base64.encodeToString("9d7dcbb0380f450ea0d2b435b60f4c15:ssEAFgv5PfEs29bluxs9N3milKgC7j6saILCtMPw".getBytes(), Base64.DEFAULT);


String query = null;
try {
query = String.format("grant_type=%s&code=%s",
URLEncoder.encode(grant_type, charset),
URLEncoder.encode(code[0], charset));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.i("eve_token_query",query);

URLConnection connection = null;
try {
connection = new URL(url).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
connection.setDoOutput(true); // Triggers POST.
//connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty( "Authorization", enc_auth);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

try (OutputStream output = connection.getOutputStream()) {
output.write(query.getBytes());
} catch (IOException e) {
e.printStackTrace();
}

for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
Log.i("eve_response_header",header.getKey() + "=" + header.getValue());
}
//InputStream response = connection.getInputStream();

} catch (Exception e) {
this.exception = e;

return null;
}

return "bla";
}

protected void onPostExecute(String... response) {
// TODO: stuff
}


}


But the answer header is this:

10-23 18:54:43.613 29926-30158/com.jbs.evecompanion I/eve_response_header: null=[HTTP/1.1 400 Bad Request]
10-23 18:54:43.613 29926-30158/com.jbs.evecompanion I/eve_response_header: Connection=[close]
10-23 18:54:43.613 29926-30158/com.jbs.evecompanion I/eve_response_header: Content-Length=[339]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: Content-Type=[text/html; charset=us-ascii]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: Date=[Sun, 23 Oct 2016 16:54:41 GMT]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: Server=[Microsoft-HTTPAPI/2.0]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: X-Android-Received-Millis=[1477241683613]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: X-Android-Response-Source=[NETWORK 400]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: X-Android-Selected-Protocol=[http/1.1]
10-23 18:54:43.614 29926-30158/com.jbs.evecompanion I/eve_response_header: X-Android-Sent-Millis=[1477241683576]


Which as you can see gives me a "Bad Request".

Answer

As it turns out the Base64 encoding did something weird.
Instead of Base64.DEFAULT I had to use Base64.NO_WRAP

so the line would be

String enc_auth = Base64.encodeToString("9d7dcbb0380f450ea0d2b435b60f4c15:ssEAFgv5PfEs29bluxs9N3milKgC7j6saILCtMPw".getBytes(), Base64.NO_WRAP);

Otherwise it would add random linebreaks in the encoded string.

Comments