我正在使用oauth2
并在android中使用Volley
网络库。我试图通过使用下面的代码获取访问令牌。但我收到400 Bad请求作为我的回复。我的要求有什么问题?我甚至尝试将内容类型设为application/x-www-form-urlencoded
,但仍然没有运气。可能是导致错误的主要原因是什么?
StringRequest accessTokenRequest = new StringRequest(Request.Method.POST, oauthConfig.getTokenEndPointUrl(), new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Log.e("AccessTokenFromLogin", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put(OAuthConstants.GRANT_TYPE, "password");
params.put(OAuthConstants.USERNAME, userEmail);
params.put(OAuthConstants.PASSWORD, userPassword);
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
if (isValid(clientId))
params.put(OAuthConstants.CLIENT_ID, clientId);
if (isValid(clientSecret))
params.put(OAuthConstants.CLIENT_SECRET, clientSecret);
if (isValid(scope))
params.put(OAuthConstants.SCOPE, scope);
Log.e("LoginActivity",clientId+" "+clientSecret+" "+scope);
return params;
}
};
Volley.newRequestQueue(this, hurlStack).add(accessTokenRequest);