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,

            <dependency>
              <groupId>com.squareup.retrofit2</groupId>
              <artifactId>retrofit</artifactId>
              <version>2.4.0</version>
            </dependency>
            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'

            //Http.java
            import retrofit2.Call;
            import retrofit2.Response;
            import retrofit2.Retrofit;
            import retrofit2.converter.gson.GsonConverterFactory;
            
            import java.io.IOException;
            
            public class Http {
                public static void main(String[] args) {
                    Retrofit retrofit = new Retrofit.Builder()
                            .addConverterFactory(GsonConverterFactory.create())
                            .baseUrl("https://myaccount.previewtechs.com/")
                            .build();
            
                    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();
                        System.out.println(response.body().getAccessToken());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            
            Now we need to create the TokenResponse.java model class.


            //TokenResponse.java
            import com.google.gson.annotations.Expose;
            import com.google.gson.annotations.SerializedName;
            
            public class TokenResponse {
            
                @SerializedName("token_type")
                @Expose
                private String tokenType;
                @SerializedName("expires_in")
                @Expose
                private Integer expiresIn;
                @SerializedName("access_token")
                @Expose
                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 AccessTokenServiceInterface.java for retrofit builder.


            //AccessTokenServiceInterface.java
            import retrofit2.Call;
            import retrofit2.http.Field;
            import retrofit2.http.FormUrlEncoded;
            import retrofit2.http.POST;
            
            public interface AccessTokenServiceInterface {
            
                @POST("/oauth/v1/access_token")
                @FormUrlEncoded
                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
            Helpful?  
            Help us to make this article better
            0 0