Preview Technologies Limited
            Knowledge Base Developers Center Authentication OAuth2.0 Client Credentials Grant

            Get access token with Retrofit 2.x - client_credentials grant type

            Retrofit is a type-safe HTTP client for Android and Java. You can easily use Retrofit to get access token from our OAuth2.0 server with simplified way. In the example below, we are going to use "Retrofit 2.x" version. So let's get started.

            Let's install Retrofit first.

            With Maven,

            And with Gradle,

            compile 'com.squareup.retrofit2:retrofit:2.4.0'
            For details installation, we would suggest you to see it's official documentation from here.

            Now, we need to create the Retrofit builder.

            compile 'com.squareup.retrofit2:retrofit:2.4.0'

            import retrofit2.Call;
            import retrofit2.Response;
            import retrofit2.Retrofit;
            import retrofit2.converter.gson.GsonConverterFactory;
            public class Http {
                public static void main(String[] args) {
                    Retrofit retrofit = new Retrofit.Builder()
                    AccessTokenServiceInterface service = retrofit.create(AccessTokenServiceInterface.class);
                    Call call = service.getToken("OAUTH_CLIENT_ID", "OAUTH_CLIENT_SECRET", "SPACE SEPARATED SCOPES", "client_credentials");
                    try {
                        Response response = call.execute();
                    } catch (IOException e) {
            Now we need to create the model class.

            public class TokenResponse {
                private String tokenType;
                private Integer expiresIn;
                private String accessToken;
                public String getTokenType() {
                    return tokenType;
                public void setTokenType(String tokenType) {
                    this.tokenType = tokenType;
                public Integer getExpiresIn() {
                    return expiresIn;
                public void setExpiresIn(Integer expiresIn) {
                    this.expiresIn = expiresIn;
                public String getAccessToken() {
                    return accessToken;
                public void setAccessToken(String accessToken) {
                    this.accessToken = accessToken;
            Now we need to create for retrofit builder.

            import retrofit2.Call;
            import retrofit2.http.Field;
            import retrofit2.http.FormUrlEncoded;
            import retrofit2.http.POST;
            public interface AccessTokenServiceInterface {
                Call getToken(@Field("client_id") String client_id, @Field("client_secret") String client_secret, @Field("scope") String scope, @Field("grant_type") String grant_type);
            That's it. Now run the Http.main and you will get the access token easily. Download these scripts from our PreviewTechnologies/access-token-retrofit GitHub repository. Thanks @shaharia for making this implementation.
            Updated: 15 Apr 2018 02:38 PM
            Help us to make this article better
            0 0