OkHttp OAuth2 client


Source link: https://github.com/corcoran/okhttp-oauth2-client

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)

Gradle

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.3.0' 
}

Usage

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

Callbacks

client.requestAccessToken(new OAuthResponseCallback() {

  @Override
  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")

.grantType("grant-type")

.scope("scope")

.username("username")

.password("password");

Provide your own OkHttpClient to the builder

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

.okHttpClient(client);

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

.parameters(map)

Wrap with RxJava!

OAuth2Client.Builder builder = new OAuth2Client.Builder("client-id", "client-secret", "http://localhost:8000/auth/token");
 final OAuth2Client client = builder.build();
  Observable<OAuthResponse> observable = Observable.fromCallable(new Callable<OAuthResponse>() {

  @Override
  public OAuthResponse call() throws Exception {

return client.refreshAccessToken("refresh-token");

  
}
 
}
);
  observable.subscribe(new Action1<OAuthResponse>() {

  @Override
  public void call(OAuthResponse oAuthResponse) {

oAuthResponse.getAccessToken();

  
}
 
}
);

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?

Acknowledgments

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

License

Copyright 2017 Jeff Corcoran  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
  http://www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 

Resources

similarLoadingView is a custom loading view for Android. It is very easy to use and customize. It draws your drawable on canvas with stylish design.

PseudoNet is a app which integrates various online services into a single app and provides a user interface to use those services offline using SMS.

Mediation advertise helper for ADMOB, FACEBOOK - Support Native, Banner, Backpress Dialog, Front AD.

A demo that shows how to implement chained spring animation on Android.

An Android frame In order to support single Activity + multi Fragment framework.

Abstraction to build adapters with dynamic/variable view types count for Android application. It eliminates the need to manually define view types. Makes code readable and actually share view types across multiple screens (no need to write a new adapter, or add view types handling in one base adapter with endless switch statement).

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes