Awesome Open Source
Awesome Open Source

OkHttp OAuth2 client

A modern Android oAuth2 library using OkHttp with resource owner password grant types and easy token refreshing. This library aims to provide a solution for the less commonly used resource owner password grant type as well as providing dynamic parameter support that can be used with frameworks that allow for more flexible and dynamic oAuth2 parameters (such as the Django REST framework social oAuth2 library)

License Release Download


The Gradle dependency is available via jCenter. jCenter is the default Maven repository used by Android Studio.

dependencies {
    // ... other dependencies here
    compile 'ca.mimic:oauth2library:2.4.2'


OAuth2Client client = new OAuth2Client.Builder("username", "password", "client-id", "client-secret", "site").build();
OAuthResponse response = client.requestAccessToken();

if (response.isSuccessful()) {
    String accessToken = response.getAccessToken();
    String refreshToken = response.getRefreshToken();
} else {
    OAuthError error = response.getOAuthError();
    String errorMsg = error.getError();

response.getCode();   // HTTP Status code

To refresh a token (defaults to "refresh_token" grant type)

OAuth2Client client = new OAuth2Client.Builder("client-id", "client-secret", "site").build();
OAuthResponse response = client.refreshAccessToken("refresh-token");
String accessToken = response.getAccessToken();


client.requestAccessToken(new OAuthResponseCallback() {
    public void onResponse(OAuthResponse response) {
        if (response.isSuccessful()) {
            // response.getAccessToken();
        } else {
            // response.getOAuthError();

Builder options and parameters

Parameters for the builder

OAuth2Client.Builder builder = new OAuth2Client.Builder("client-id", "client-secret", "site")

Provide your own OkHttpClient to the builder

OkHttpClient client = new OkHttpClient();

OAuth2Client.Builder builder = new OAuth2Client.Builder("client-id", "client-secret", "site")

Provide additional name / value string parameters

Map<String, String> map = new HashMap<>();
map.put("backend", "example-backend");

OAuth2Client.Builder builder = new OAuth2Client.Builder("client-id", "client-secret", "site")

Wrap with RxJava!

OAuth2Client.Builder builder = new OAuth2Client.Builder("client-id", "client-secret", "http://localhost:8000/auth/token");
final OAuth2Client client =;

Observable<OAuthResponse> observable = Observable.fromCallable(new Callable<OAuthResponse>() {
    public OAuthResponse call() throws Exception {
        return client.refreshAccessToken("refresh-token");

observable.subscribe(new Action1<OAuthResponse>() {
    public void call(OAuthResponse oAuthResponse) {

Extra response data

OAuthResponse contains other potentially helpful data

OAuthResponse response = client.requestAccessToken();
response.getHttpResponse();   // OkHttp response
response.getBody();           // Response body string
response.isJsonResponse();    // Was JSON parsed?


This library was inspired by the android-oauth2-client library by Daniel Szmulewicz


Copyright 2018 Mimic Mobile

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
java (31,769
android (6,472
rxjava (332
oauth2 (315
okhttp3 (58
oauth2-client (30