At the moment I am trying to change my Api code from volley to retrofit. The tutorials are simple but for some reason, when I do the api call on Volley it works and when I call it on Retrofit it doesn't. Before I start the api call I authorize myself with Oauth2 and a Webview. The webview hands me a authorization code which I then try to convert into a access_token. Below you can see the simple code with Volley:
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url2 ="...oauth/token";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url2,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "onResponse: " + response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("grant_type" , "authorization_code");
params.put("client_id" , "...");
params.put("client_secret" , "...");
params.put("redirect_uri" , "...");
params.put("code" , code);
params.put("scope" , "*");
return params;
}
};
// Add the request to the RequestQueue.
queue.add(stringRequest);
Below you can see the retrofit code:
Interface:
@POST("oauth/token")
Call<ResponseBody> getToken(@Query("grant_type") String grantType,@Query("client_id") String clientId , @Query("client_secret") String clientSecret,
@Query("redirect_uri") String redirectURI, @Query("code") String code,@Query("scope") String scope);
BaseApi:
retrofitInstance = new Retrofit.Builder()
.baseUrl(CONSTANT_API_BASE_URL_STAGING)
.build();
apiServiceInstance = retrofitInstance.create(ApiService.class);
Implementation:
Call<ResponseBody> s = baseApi.apiServiceInstance.getToken("authorization_code" ,"...",
"...",
"...",
code,"*");
s.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d(TAG, "onResponse: "+response);
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
When I call the volley code it returns an Array as a string which gives me the refresh_token, access_token and the expire_date
When I call the Retrofit code it returns {protocol=http/1.1, code=400, message=Bad Request, url=...}