来自 curl 命令的 Volley String post 请求
Volley String post request from a curl command
我正在尝试 access-token。通过使用 curl/commnad window 我可以得到它,但我需要使用 android 截击。 Volley 显示错误说意外的响应代码。这是 curl 命令:
$ curl "https://api.*****.com/auth/oauth/v2/token" \
--不安全\
--header "Accept: 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。但这并没有解决我的问题。再次感谢
我解决了这个问题。方法如下:
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
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("grant_type","authorization_code");
params.put("code",preference.getGrantCode());
params.put("client_id", Resources.CLIENT_ID);
params.put("client_secret",Resources.CLIENT_SECRET);
params.put("scope","scope_test");
params.put("state","state_test");
params.put("redirect_uri",Resources.REDIRECT_URI );
return params;
}
@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);
我运行遇到了类似的问题。
将 --data 值放入 body 为我解决了。
我正在尝试 access-token。通过使用 curl/commnad window 我可以得到它,但我需要使用 android 截击。 Volley 显示错误说意外的响应代码。这是 curl 命令:
$ curl "https://api.*****.com/auth/oauth/v2/token" \
--不安全\
--header "Accept: 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。但这并没有解决我的问题。再次感谢
我解决了这个问题。方法如下:
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
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<String,String>();
params.put("grant_type","authorization_code");
params.put("code",preference.getGrantCode());
params.put("client_id", Resources.CLIENT_ID);
params.put("client_secret",Resources.CLIENT_SECRET);
params.put("scope","scope_test");
params.put("state","state_test");
params.put("redirect_uri",Resources.REDIRECT_URI );
return params;
}
@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);
我运行遇到了类似的问题。 将 --data 值放入 body 为我解决了。