1

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=...}

1 Answers1

1

Below you can see the retrofit code.

Dependencies:

implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.0'

Base API:-

private static Retrofit retrofit;
    public static Retrofit getClient() {
        OkHttpClient unsafeOkHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient();

        if (retrofit == null) {

            retrofit =newRetrofit.Builder().baseUrl(BASEURL).client(unsafeOkHttpClient).addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }

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);

Implementation:

public RequestAPI requestAPI = (RequestAPI) ApiClient.getClient().create(RequestAPI.class);

public void getResponse(){
  this.requestAPI.getToken("authorization_code" ,"...",
                                "...",
                                "...",
                                code,"*");.enqueue(this.GetTokenCallback);

  }
  
Callback<ResponseBody> GetCategoryCallback = new Callback<ResponseBody>() {
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            ResponseBody body = response.body();

            Log.e("RESPONSE", "Token: " + new Gson().toJson(response.body()));
        }
        public void onFailure(Call<ResponseBody> call, Throwable th) {
            Toast.makeText(activity, "Something went wrong..." + th.getMessage(), 
            Toast.LENGTH_LONG).show();

        }
    };
Daddys Code
  • 581
  • 2
  • 8