【发布时间】:2019-03-21 12:14:19
【问题描述】:
我正在尝试获取访问令牌。通过使用 curl/commnad 窗口我可以得到它,但我需要使用 android volley。 Volley 显示错误表示意外响应代码。这是 curl 命令:
$ curl "https://api.*****.com/auth/oauth/v2/token" \
--不安全的\
--header "接受: application/json" \
--header "Content-Type: application/x-www-form-urlencoded" \
--数据 "grant_type=authorization_code&code=9d40008a9a4-438b-800c-dec6486a7631"\
--数据 “client_id=l7xxdedc2c3d49e58459287fe66092ad&client_secret=fa48a352e633841695b1d969750” \
--数据 "scope=scope_test&state=state_test" \
--数据“redirect_uri=http%3A%2F%2Flocalhost%3A3000”\
--请求POST$
我写了一个等效的 Volley 字符串请求。我不知道我的错误在哪里?为什么它没有给我回应。请帮帮我
StringRequest postRequest=new StringRequest(Request.Method.POST,url,new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("accessToken:",response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("error:",volleyError.toString());
}
}){
@Override
public byte[] getPostBody() throws AuthFailureError {
String grantcode=preference.getAccessToken();
String httpPostBody="grant_type=authorization_code&code="+grantcode+"&client_id=l7xx5a227281eb364ab3bb6fd8cc49648427&client_secret=c29a24fad90640f993770f07a5e77892&scope=scope_test&state=state_test";
return httpPostBody.getBytes();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> headers=new HashMap<String,String>();
headers.put("Accept","application/json");
headers.put("Content-Type","application/x-www-form-urlencoded");
return headers;
}
};
ApplicationController.getInstance().addToRequestQueue(postRequest);
}
});
即使我尝试使用 getBody 代替 getPostBody。但这并没有解决我的问题。再次感谢
【问题讨论】:
标签: android-volley