Skip to main content

Android

配置Authok

获取应用密钥

你需要如下信息

  • Domain
  • Client ID
  • Client Secret

配置回调URL

配置 Logout URL

安装 AuthOK Android SDK

Grale

dependencies {
// Add the Authok Android SDK
implementation 'com.authok.android:authok:1.+'
}

配置 Java 8 字节码 和 Kotlin 插件:

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
tip

记得在 Android Studio 中 同步 Gradle 项目, 或者运行./gradlew clean build命令.

添加 manifest 占位符:

app/build.gradle
apply plugin: 'com.android.application'
compileSdkVersion 30
android {
defaultConfig {
applicationId "com.authok.samples"
minSdkVersion 21
targetSdkVersion 30
// ...

// ---> Add the next line
manifestPlaceholders = [authokDomain: "@string/com_authok_domain", authokScheme: "demo"]
// <---
}
}

AndroidManifest.xml中添加 android.permissions.INTERNET权限:

<uses-permission android:name="android.permission.INTERNET" />

添加登录

onCreate方法中,创建Authok实例:

import com.authok.android.Authok
import com.authok.android.provider.WebAuthProvider

class MainActivity : AppCompatActivity() {

private lateinit var account: Authok

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// Set up the account object with the Authok application details
account = Authok(
"YOUR_CLIENT_ID",
"YOUR_DOMAIN"
)
}
}

使用WebAuthProvider创建一个loginWithBrowsser方法:

private fun loginWithBrowser() {
// Setup the WebAuthProvider, using the custom scheme and scope.

WebAuthProvider.login(account)
.withScheme("demo")
.withScope("openid profile email")
// Launch the authentication passing the callback where the results will be received
.start(this, object : Callback<Credentials, AuthenticationException> {
// Called when there is an authentication failure
override fun onFailure(exception: AuthenticationException) {
// Something went wrong!
}

// Called when authentication completed successfully
override fun onSuccess(credentials: Credentials) {
// Get the access token from the credentials object.
// This can be used to call APIs
val accessToken = credentials.accessToken
}
})
}

当你调用WebAuthProvider#start函数,浏览器会启动并显示登录页面. 当用户认证成功,会调用回调URL. 回调URL中包含认证处理的结果.

添加注销

private fun logout() {
WebAuthProvider.logout(account)
.withScheme("demo")
.start(this, object: Callback<Void?, AuthenticationException> {
override fun onSuccess(payload: Void?) {
// The user has been logged out!
}

override fun onFailure(error: AuthenticationException) {
// Something went wrong!
}
})
}

显示用户信息

private fun showUserProfile(accessToken: String) {
var client = AuthenticationAPIClient(account)

// With the access token, call `userInfo` and get the profile from Authok.
client.userInfo(accessToken)
.start(object : Callback<UserProfile, AuthenticationException> {
override fun onFailure(exception: AuthenticationException) {
// Something went wrong!
}

override fun onSuccess(profile: UserProfile) {
// We have the user's profile!
val email = profile.email
val name = profile.name
}
})
}