我谷歌,我试图通过附加标题发布字符串请求。但我总是得到auth失败错误。似乎我的标题设置不正确。这是我的代码
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("response",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error!=null){
error.printStackTrace();
}
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("client_id=",getString(R.string.client_id));
params.put("&client_secret=",getString(R.string.client_secret));
params.put("&grant_type=","authorization_code");
params.put("&code=",accessToken);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> params = new HashMap<String, String>();
params.put("Authorization", "Basic " + base64);
params.put("Content-Type", "application/x-www-form-urlencoded");
params.put("Accept", "*/*" );
return super.getHeaders();
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
我在getHeaders()中附加了标头,并在getParams()中附加了编写器。有人可以帮我解决这个问题。提前谢谢。
答案 0 :(得分:1)
不要像这样设置params
params.put("&grant_type=","authorization_code");
像这样替换
params.put("grant_type", authorization_code);
不要添加&
和=
签名,它会自动包含在内。
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("client_id",getString(R.string.client_id));
params.put("client_secret",getString(R.string.client_secret));
params.put("grant_type","authorization_code");
params.put("code",accessToken);
return params;
}
尝试像这样的标题
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap();
headers.put("appId", "MYAPP_ID_HERE");
return headers;
}