在Volley HTTP POST请求中设置简单的字符串头(而不是键/值对)



EDIT:看起来主要问题是设置一个没有键/值对和相关分隔符的纯字符串标头,因为运行一个没有"它给了我一个服务器响应,因此我编辑了标题。

我正试图使用Volley从安卓应用程序发送一个POST请求,如本亚马逊Alexa教程中所述进行身份验证。

对于我正在发送的第二个请求(需要一个标头(,我收到一个400服务器错误,表示请求不正确。

根据教程页面,请求头应该是这样的:

请求必须包括以下标头:

POST /auth/o2/token
Host: api.amazon.com
Content-Type: application/x-www-form-urlencoded

如果我使用Volley-Request类的常规getHeaders((方法来覆盖标头,我只能设置一个hashmap,这会导致以下标头格式:

{Host=api.amazon.com, Content-Type=application/x-www-form-urlencoded}

(或{POST=/auth/o2/token, Host=api.amazon.com, Content-Type=application/x-www-form-urlencoded},如果我包括第一位的另一行(

作为沃尔利的新手,我想知道我是否错过了一些明显的东西。这是我发送的请求:

StringRequest tokenPoller = new StringRequest(
Request.Method.POST,
"https://api.amazon.com/auth/O2/token",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("volley", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
error.printStackTrace();
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "device_code");
params.put("device_code", {{my device code from previous request}});
params.put("user_code", {{my user code from previous request}});
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String>  headers = new HashMap<String, String>();
headers.put("Host", "api.amazon.com");
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};

我怀疑一些关于标题的问题,但我真的无法确定。我也尝试过不覆盖标题,但没有成功。任何指点都将不胜感激!谢谢

本例中的400响应并不意味着我的请求格式不正确。这只是服务器返回的响应代码,其中包含与授权过程相关的所有错误,在我的情况下,例如authorization_pending。由于我没有请求错误消息(通过Volley或其他方式(,我直到很久以后才看到。

虽然本教程确实提到了在轮询令牌时可以传递几种类型的响应,但它并没有提到它们实际上是400响应的错误和错误描述。

我最终从Volley切换到了HttpsUrlConnection,并使用这个问题的响应来接收作为json响应的错误和错误消息,并实现相应的响应。

最后,我的HTTP请求看起来是这样的:

String stringUrl = "https://api.amazon.com/auth/o2/token";
URL url = null;
try {
url = new URL(stringUrl);
} catch (MalformedURLException exception) {
Log.e(TAG, "Error with creating URL", exception);
}
String response = "";
HttpsURLConnection conn = null;
HashMap<String, String> params = new HashMap<String, String>();
String scope_data = null;

try {
params.put("grant_type", "device_code");
params.put("device_code", mDeviceCode);
params.put("user_code", mUserCode);
Set set = params.entrySet();
Iterator i = set.iterator();
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, String> param : params.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");

conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setDoOutput(true);
conn.getOutputStream().write(postDataBytes);
InputStream inputStream = null;
try {
inputStream = conn.getInputStream();
} catch(IOException exception) {
inputStream = conn.getErrorStream();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("n");
}
reader.close();
conn.disconnect();
response = builder.toString();

最新更新