United121
United121

Reputation: 729

Convert cURL to Retrofit

I am trying to implement OAuth2. I need to use it for my school project - the school provide some documentation and it say that I need to call this

POST /oauth/token HTTP/1.1
Host: oaas.example.org
Authorization: Basic ZHVtbXktY2xpZW50OnRvcC1zZWNyZXQ=
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth

or there is sample with curl

curl --data "grant_type=authorization_code&code=l1kSf2&redirect_uri=https://client.example.org/auth" \
     --user dummy-client:top-secret https://oaas.example.org/oauth/token

But I dont know how to transfer it to Android calling. I am using Retrofit currently and reguest is like JSON :

POST /oauth/oauth/token/DeviceAndroid HTTP/1.1
Authorization: Basic OGRkOGJiZGMtOGI2NC00NTdlLTgwYWMtZmE5NDZjNzY4Njgzc0owY3RKV2VWNHFLY0dwWHNIOGEzcDVYYUl0NDRGN2o=
Content-Type: application/x-www-form-urlencoded
Content-Length: 85
Host: auth.fit.cvut.cz
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/2.5.0

{"code":"p1P3os","grant_type":"authorization_code","redirect_uri":"http://localhost"}

My network interface

public interface NetworkInterface {
    String TAG = NetworkInterface.class.getName();

    String URL = "https://auth.fit.cvut.cz";

    @Headers("Content-Type: application/x-www-form-urlencoded")
    @POST("/oauth/oauth/token/DeviceAndroid")
    Call<Object> sendInformationToServer(@Body RequestToken requestToken);
}

RequestToken

public class RequestToken {
    public static final String TAG = RequestToken.class.getName();

    public String grant_type;
    public String code;
    public String redirect_uri;
    ...

Service generator

public class ServiceGenerator {

    private static OkHttpClient httpClient = new OkHttpClient();
    private static Retrofit.Builder builder =
        new Retrofit.Builder()
                .baseUrl(NetworkInterface.URL)
                .addConverterFactory(GsonConverterFactory.create());

public static <T> T createService(Class<T> serviceClass , final String autorizationString) {
    httpClient = new OkHttpClient();
    if (autorizationString != null) {
        httpClient.interceptors().clear();
        httpClient.interceptors().add(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request original = chain.request();

                Request.Builder requestBuilder = original.newBuilder()
                        .header("Content-Type", "application/x-www-form-urlencoded")
                        .header("Authorization", "Basic " + RequestToken.returnBase64(autorizationString))
                        .method(original.method(), original.body());

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });
    }

    Retrofit retrofit = builder.client(httpClient).build();
    return retrofit.create(serviceClass);
}
}

Is there a way to call it right with Retrofit (or with different approach), Thanks in advance

Upvotes: 3

Views: 6061

Answers (1)

Nick Cardoso
Nick Cardoso

Reputation: 21763

It seems like your call should look like the following

public interface NetworkInterface {

    private String TAG = NetworkInterface.class.getName();
    public statsic final String ROOT_URL = "https://auth.fit.cvut.cz";

    @FormUrlEncoded
    @POST("/oauth/token")
    Call<Object> sendInformationToServer(
            @Field("code") String code,
            @Field("grant_type") String grant,
            @Field("redirect_uri") String redirect,
    );

}

Used like this:

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(NetworkInterface.ROOT_URL)
 ...
.build();

NetworkInterface service = retrofit.create(NetworkInterface .class);

service.sendInformationToServer("p1P3os", "authorization_code", "http://localhost"})

Upvotes: 2

Related Questions