百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

okhttp-oauth2-client

> 安全
开源

使用 OkHttp 的 Android OAuth2 客户端

196 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

使用 OkHttp 的 Android OAuth2 客户端

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.

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

Usage

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

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

Callbacks

java
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

java
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

java
OkHttpClient client = new OkHttpClient();

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

Provide additional name / value string parameters

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

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

Wrap with RxJava!

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

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

observable.subscribe(new Action1() {
    @Override
    public void call(OAuthResponse oAuthResponse) {
        oAuthResponse.getAccessToken();
    }
});

Extra response data

OAuthResponse contains other potentially helpful data

java
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 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

   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.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Javaandroidoauth2oauth2-clientokhttp3

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类安全
定价开源

> 相关工具

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.