This is a OAuth2 authentication client for the Reddit API built for Android.
This library is used as a base for this API wrapper:
https://github.com/KirkBushman/ARAW
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
dependencies {
implementation 'com.github.KirkBushman:Android-Reddit-OAuth2:Tag'
}
-
Register your application at: https://www.reddit.com/prefs/apps
-
Create the manager object that is responsible for the initial interaction with the browser and the retrieval of the token. The clientId and redirectUrl should match what you wrote on the reddit api console, for more info refer to this: https://github.com/reddit-archive/reddit/wiki/oauth2
val authClient = RedditAuth.Builder()
// specify the credentials you can find on your reddit app console
.setApplicationCredentials(creds.clientId, creds.redirectUrl)
// the api endpoints scopes this client will need
.setScopes(arrayOf("Input", "scopes", "here"))
// to manage tokens info in memory
.setStorageManager(SharedPrefsStorageManager(this))
// if you set this flag to 'true' it will add to the OkHttp Client a listener to log the
// Request and Response object, to make it easy to debug.
.setLogging(true)
.build()
-
You need to make your user input his username and password, and this is done with a webView. The manager object we built in step 1 will provide the url:
browser.loadUrl(authClient.provideAuthorizeUrl())
-
We'll be watching for any redirects, so implement a webViewClient like this:
val browser = WebView(context)
browser.webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
if (authClient.isRedirectedUrl(url)) {
browser.stopLoading()
// We will retrieve the bearer on the background thread.
doAsync(doWork = {
// This is the tokenBearer object, that will manage your token. Done.
val bearer = authClient.getTokenBearer(url)
})
}
}
}
- The token bearer is what will help you keep the token updated and make requests, Enjoy.
-
Register your application at: https://www.reddit.com/prefs/apps
-
Create the auth manager object like so:
val authClient = RedditAuth.Builder()
// specify the credentials you can find on your reddit app console,
// in this case only the client id is provided.
.setUserlessCredentials(creds.clientId)
// the api endpoints scopes this client will need
.setScopes(creds.scopes.toTypedArray())
// to manage tokens info in memory
.setStorageManager(SharedPrefsStorageManager(this))
// if you set this flag to 'true' it will add to the OkHttp Client a listener to log the
// Request and Response object, to make it easy to debug.
.setLogging(true)
.build()
- Simply use this code, to get your token bearer:
bearer = authClient?.getTokenBearer()
- Enjoy.
-
Register your script at: https://www.reddit.com/prefs/apps, make sure that your account is a
developer
for that script. -
Create the auth manager object like so:
val authClient = RedditAuth.Builder()
// specify the credentials you can find on your reddit app console
// for script / web apps you will be given a client id and a client secret
.setCredentials(creds.username, creds.password, creds.scriptClientId, creds.scriptClientSecret)
// the api endpoints scopes this client will need
.setScopes(creds.scopes.toTypedArray())
// to manage tokens info in memory
.setStorageManager(SharedPrefsStorageManager(this))
// if you set this flag to 'true' it will add to the OkHttp Client a listener to log the
// Request and Response object, to make it easy to debug.
.setLogging(true)
.build()
- Simply use this code, to get your token bearer:
bearer = authClient?.getTokenBearer()
- Enjoy.
When you are done using any token, rember to revoke them, and not let them around!
This project is licensed under the MIT License