Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stantanasi/retrofit-oauth2-converter
Retrofit OAuth2 Converter is an Android library for converting OAuth2 response to data class
https://github.com/stantanasi/retrofit-oauth2-converter
android android-library converter kotlin oauth2 oauth2-client retrofit retrofit2
Last synced: 4 days ago
JSON representation
Retrofit OAuth2 Converter is an Android library for converting OAuth2 response to data class
- Host: GitHub
- URL: https://github.com/stantanasi/retrofit-oauth2-converter
- Owner: stantanasi
- License: apache-2.0
- Created: 2021-04-16T09:54:10.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-10-06T21:21:58.000Z (over 3 years ago)
- Last Synced: 2024-11-10T07:37:45.136Z (2 months ago)
- Topics: android, android-library, converter, kotlin, oauth2, oauth2-client, retrofit, retrofit2
- Language: Kotlin
- Homepage:
- Size: 146 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Retrofit OAuth2 converter
Retrofit OAuth2 Converter is a Android library for convert OAuth2 response to data class
# Introduction
### OAuth2
OAuth 2 is an authorization framework that enables applications to obtain limited access to user accounts on an HTTP service, such as Facebook, GitHub, and DigitalOcean. It works by delegating user authentication to the service that hosts the user account, and authorizing third-party applications to access the user account. OAuth 2 provides authorization flows for web and desktop applications, and mobile devices.
# Getting started
### Implement dependency
```gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}dependencies {
implementation 'com.github.stantanasi:retrofit-oauth2-converter:LAST_VERSION'
}
```### Setup
Add the following lines when creating the retrofit instance:
+ **addCallAdapterFactory(OAuth2CallAdapterFactory.create())**
+ **addConverterFactory(OAuth2ConverterFactory.create())**
```kotlin
val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addCallAdapterFactory(OAuth2CallAdapterFactory.create())
.addConverterFactory(OAuth2ConverterFactory.create())
.build()
```# Usage
## API Service
```kotlin
@FormUrlEncoded
@POST("oauth/token")
suspend fun login(
@Field("username") username: String,
@Field("password") password: String,
@Field("grant_type") grantType: String = "password"
): OAuth2Response
```## OAuth2 success response
```kotlin
val response = TestApiService.build().login(username, password)
when (response) {
is OAuth2Response.Success -> {
response.headers // okhttp3.Headers
response.code // Int = 2xxresponse.body.accessToken
response.body.tokenType
response.body.expiresIn
response.body.refreshToken
response.body.scoperesponse.body.raw // String
}
is OAuth2Response.Error.ServerError -> TODO()
is OAuth2Response.Error.NetworkError -> TODO()
is OAuth2Response.Error.UnknownError -> TODO()
}
```## Error response
```kotlin
val response = TestApiService.build().login(username, password)
when (response) {
is OAuth2Response.Success -> TODO()
is OAuth2Response.Error.ServerError -> {
when (response.body) {
is OAuth2ErrorBody.InvalidClient -> {
response.body.description
response.body.uri
}
is OAuth2ErrorBody.InvalidGrant -> TODO()
is OAuth2ErrorBody.InvalidRequest -> TODO()
is OAuth2ErrorBody.InvalidScope -> TODO()
is OAuth2ErrorBody.UnauthorizedClient -> TODO()
is OAuth2ErrorBody.UnsupportedGrantType -> TODO()
}
}
is OAuth2Response.Error.NetworkError -> {
response.error // IOException
Log.e("TAG", "getArticle: ", response.error)
}
is OAuth2Response.Error.UnknownError -> {
response.error // Throwable
Log.e("TAG", "getArticle: ", response.error)
}
}
```