1
服務器有Django Rest框架。我有用戶名,密碼,client_id和client_secret。現在我需要使用這些證書來生成訪問令牌。如何使用(Android)抽象向服務器發送請求,以便我可以使用令牌獲得響應。令牌響應從排隊服務器到Django rest框架
(樣本響應)
{"access_token": "123", "token_type": "Bearer", "expires_in": 36000, "refresh_token": "123", "scope": "read write groups"}
訪問令牌生成過程 -
curl -X POST -d "grant_type=password&username=**&password=**" -u "client_id:client_secret"
url: http://ip:port/oAuthTokenGeneration/token/
在這裏我都試過了,但得到com.android.volley.ServerError
queue = VolleyHandler.getRequestQueue();
String url="http://ip:port/oAuthTokenGeneration/token/";
StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("access_token:", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("error:","Error-------"+ volleyError.toString());
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type","password");
params.put("username","root");
params.put("password","root");
params.put("client_id","id");
params.put("client_secret", "secret");
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json;");
return headers;
}
};
queue.add(postRequest);