0
我正在嘗試爲我爲android編寫的應用程序使用oauth。我到目前爲止,正確的授權碼被返回到我的應用程序。現在需要發出一個請求來獲取訪問/刷新令牌。要做到這一點我有下面的代碼:我使用oauth post請求時獲取HTTP 400
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
}
}
但答案標題是這樣的:
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]
正如你可以看到給了我一個「錯誤的請求」。