为什么我在 Android post 之后一直收到错误 400?
Why do I keep getting Error 400 after post in Android?
我正在构建一个使用用户凭据登录 Spotify 的应用程序。我关注这个 API 调用:https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
这是实现代码:
@Override
protected Session doInBackground(String... params) {
Session session = new Session();
try {
String response = "";
URL url = new URL(GET_TOKEN_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
String credentials = params[0]+":"+params[1];
String basicAuth ="Basic "+new String(android.util.Base64.encode(credentials.getBytes(), Base64.DEFAULT));
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter(GRANT_TYPE, CLIENT_CREDENTIALS);
String query = builder.build().getEncodedQuery();
httpURLConnection.setRequestProperty(AUTH, basicAuth);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Language", "en-US");
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(POST);
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
int responseCode=httpURLConnection.getResponseCode();
session.setResponseCode(responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}else{
Log.e(getClass().getSimpleName(), "RESPONSE:" + responseCode);
// response = "Something went wrong";
}
Log.d(getClass().getSimpleName(), response);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
}
return session;
}
但无论我更改什么,我总是收到错误 400。
我做错了什么?
更新:
我按照 Spotify Developer 网页的确切语法使用在线工具执行了 curl,我仍然有 HTTP STATUS 400
尝试更正以下错误
1) 更正您 else
中的拼写错误。 else后面不能有semi-colon。
2) 删除计算内容长度并添加 header 的代码。长度应该是 body 的长度,而不是您编码的查询字符串。如果您只是删除此代码,HttpUrlConnection class 应该会为您生成此 header。
3) 将授权类型参数放在body中。那就是文档所说的地方。你在查询字符串中有它。您应该可以使用以下代码:
String body = GRANT_TYPE + "=" + CLIENT_CREDENTIALS;
byte[] bodyBytes = body.getBytes("UTF-8");
OutputStream stream = httpURLConnection.getOutputStream();
stream.write(bodyBytes);
stream.close();
我正在构建一个使用用户凭据登录 Spotify 的应用程序。我关注这个 API 调用:https://developer.spotify.com/web-api/authorization-guide/#client_credentials_flow
这是实现代码:
@Override
protected Session doInBackground(String... params) {
Session session = new Session();
try {
String response = "";
URL url = new URL(GET_TOKEN_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
String credentials = params[0]+":"+params[1];
String basicAuth ="Basic "+new String(android.util.Base64.encode(credentials.getBytes(), Base64.DEFAULT));
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter(GRANT_TYPE, CLIENT_CREDENTIALS);
String query = builder.build().getEncodedQuery();
httpURLConnection.setRequestProperty(AUTH, basicAuth);
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.setRequestProperty("Content-Language", "en-US");
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(POST);
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
int responseCode=httpURLConnection.getResponseCode();
session.setResponseCode(responseCode);
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}else{
Log.e(getClass().getSimpleName(), "RESPONSE:" + responseCode);
// response = "Something went wrong";
}
Log.d(getClass().getSimpleName(), response);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
}
return session;
}
但无论我更改什么,我总是收到错误 400。
我做错了什么?
更新:
我按照 Spotify Developer 网页的确切语法使用在线工具执行了 curl,我仍然有 HTTP STATUS 400
尝试更正以下错误
1) 更正您 else
中的拼写错误。 else后面不能有semi-colon。
2) 删除计算内容长度并添加 header 的代码。长度应该是 body 的长度,而不是您编码的查询字符串。如果您只是删除此代码,HttpUrlConnection class 应该会为您生成此 header。
3) 将授权类型参数放在body中。那就是文档所说的地方。你在查询字符串中有它。您应该可以使用以下代码:
String body = GRANT_TYPE + "=" + CLIENT_CREDENTIALS;
byte[] bodyBytes = body.getBytes("UTF-8");
OutputStream stream = httpURLConnection.getOutputStream();
stream.write(bodyBytes);
stream.close();