mirror of
https://github.com/logto-io/logto.git
synced 2024-12-16 20:26:19 -05:00
docs: android sdk integration guide (zh_CN) (#770)
This commit is contained in:
parent
20e839ed96
commit
18ed453628
2 changed files with 285 additions and 82 deletions
|
@ -7,27 +7,26 @@ import TabItem from '@theme/TabItem';
|
|||
|
||||
# Integrate `io.logto.sdk:android`
|
||||
|
||||
<Step
|
||||
title="Install SDK"
|
||||
subtitle="Install Logto Android SDK with Gradle"
|
||||
index={0}
|
||||
activeIndex={props.activeStepIndex}
|
||||
invalidIndex={props.invalidStepIndex}
|
||||
onNext={() => props.onNext(1)}
|
||||
>
|
||||
|
||||
## Step1: Install Logto Android SDK with Gradle
|
||||
|
||||
### Prerequisite
|
||||
* Minimal Android SDK: Level 24 (TBD)
|
||||
* Minimal Android SDK: Level 24
|
||||
|
||||
Add the `mavenCentral()` repository to your Gradle project build file:
|
||||
```kotlin
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
```
|
||||
|
||||
Add the Logto Android SDK to your dependencies:
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("io.logto.sdk:android:1.0.0")
|
||||
implementation("io.logto.sdk:android:1.0.0")
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -37,7 +36,7 @@ dependencies {
|
|||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'io.logto.sdk:android:1.0.0'
|
||||
implementation 'io.logto.sdk:android:1.0.0'
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -45,16 +44,6 @@ dependencies {
|
|||
|
||||
</Tabs>
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Configure"
|
||||
subtitle="Configure your application and LogtoClient"
|
||||
index={1}
|
||||
activeIndex={props.activeStepIndex}
|
||||
invalidIndex={props.invalidStepIndex}
|
||||
onNext={() => props.onNext(2)}
|
||||
>
|
||||
|
||||
## Step2: Configure
|
||||
|
||||
|
@ -87,15 +76,15 @@ import io.logto.sdk.android.LogtoClient
|
|||
import io.logto.sdk.android.type.LogtoConfig
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
val logtoConfig = LogtoConfig(
|
||||
endpoint = "<your-logto-endpoint>",
|
||||
appId = "<your-app-id>",
|
||||
scopes = null,
|
||||
resources = null,
|
||||
usingPersistStorage = true,
|
||||
)
|
||||
val logtoConfig = LogtoConfig(
|
||||
endpoint = "<your-logto-endpoint>",
|
||||
appId = "<your-app-id>",
|
||||
scopes = null,
|
||||
resources = null,
|
||||
usingPersistStorage = true,
|
||||
)
|
||||
|
||||
val logtoClient = LogtoClient(logtoConfig, application)
|
||||
val logtoClient = LogtoClient(logtoConfig, application)
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -108,22 +97,22 @@ import io.logto.sdk.android.LogtoClient;
|
|||
import io.logto.sdk.android.type.LogtoConfig;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private LogtoClient logtoClient;
|
||||
private LogtoClient logtoClient;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
LogtoConfig logtoConfig = new LogtoConfig(
|
||||
"<your-logto-endpoint>",
|
||||
"<your-app-id>",
|
||||
null,
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
logtoClient = new LogtoClient(logtoConfig, getApplication());
|
||||
LogtoConfig logtoConfig = new LogtoConfig(
|
||||
"<your-logto-endpoint>",
|
||||
"<your-app-id>",
|
||||
null,
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
logtoClient = new LogtoClient(logtoConfig, getApplication());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -137,16 +126,6 @@ Notes:
|
|||
- `<your-logto-endpoint>` is the logto service running in your machine. If you start logto service in `http://localhost:300` then `<your-logto-endpoint` will be `http://localhost:300`
|
||||
- `<your-app-id>` is the Application ID of your application, which is created in the Admin Console.
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign In"
|
||||
subtitle="Sin In to your application by Logto and do some extra works"
|
||||
index={2}
|
||||
activeIndex={props.activeStepIndex}
|
||||
invalidIndex={props.invalidStepIndex}
|
||||
onNext={() => props.onNext(3)}
|
||||
>
|
||||
|
||||
## Step3: Sign In
|
||||
|
||||
|
@ -192,15 +171,15 @@ logtoClient.signInWithBrowser(
|
|||
|
||||
```kotlin
|
||||
logtoClient.getAccessToken { logtoException: LogtoException?, result: AccessToken? ->
|
||||
// custom logic
|
||||
// custom logic
|
||||
}
|
||||
|
||||
logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenCliams? ->
|
||||
// custom logic
|
||||
logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenClaims? ->
|
||||
// custom logic
|
||||
}
|
||||
|
||||
logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: UserInfoResponse? ->
|
||||
// custom logic
|
||||
// custom logic
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -210,7 +189,7 @@ logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: U
|
|||
|
||||
```java
|
||||
logtoClient.getAccessToken((logtoException, accessToken) -> {
|
||||
// custom logic
|
||||
// custom logic
|
||||
});
|
||||
|
||||
logtoClient.getIdTokenClaims((logtoException, idTokenClaims) -> {
|
||||
|
@ -226,15 +205,6 @@ logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> {
|
|||
|
||||
</Tabs>
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Sign Out"
|
||||
index={3}
|
||||
activeIndex={props.activeStepIndex}
|
||||
invalidIndex={props.invalidStepIndex}
|
||||
onNext={() => props.onNext(4)}
|
||||
>
|
||||
|
||||
## Step4: Sign Out
|
||||
|
||||
|
@ -246,7 +216,7 @@ logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> {
|
|||
|
||||
```kotlin
|
||||
logtoClient.signOut { logtoException: LogtoException? ->
|
||||
// custom logic
|
||||
// custom logic
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -268,22 +238,9 @@ Notes:
|
|||
|
||||
- `signOut` will always clear local credentials even if errors occurred.
|
||||
|
||||
</Step>
|
||||
|
||||
<Step
|
||||
title="Further Readings"
|
||||
subtitle="3 steps"
|
||||
index={4}
|
||||
activeIndex={props.activeStepIndex}
|
||||
invalidIndex={props.invalidStepIndex}
|
||||
buttonText="general.done"
|
||||
buttonHtmlType="submit"
|
||||
>
|
||||
|
||||
## Step5: Further Readings
|
||||
|
||||
- Fetch User Info
|
||||
- Configure Social Sign-In
|
||||
- Access Protected API Resources
|
||||
|
||||
</Step>
|
||||
|
|
|
@ -0,0 +1,246 @@
|
|||
---
|
||||
sidebar_label: Android
|
||||
---
|
||||
|
||||
import Tabs from '@theme/Tabs';
|
||||
import TabItem from '@theme/TabItem';
|
||||
|
||||
# 集成 `io.logto.sdk:android`
|
||||
|
||||
## 步骤 1: 从 Gradle 安装 Logto Android SDK
|
||||
|
||||
### 前提条件
|
||||
* 最小 Android SDK 版本: Level 24
|
||||
|
||||
将 `mavenCentral()` 添加到构建脚本中:
|
||||
```kotlin
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
```
|
||||
|
||||
添加 Logto Android SDK 依赖:
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
dependencies {
|
||||
implementation("io.logto.sdk:android:1.0.0")
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
<TabItem value="groovy" label="Groovy">
|
||||
|
||||
```groovy
|
||||
dependencies {
|
||||
implementation 'io.logto.sdk:android:1.0.0'
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
||||
|
||||
## 步骤 2: 配置
|
||||
|
||||
### 配置 Redirect URI
|
||||
|
||||
Redirect URI 指定了应用用来接受授权结果的端口,Logto Android SDK 内部实现了该重定向的功能
|
||||
|
||||
### 在管理员控制台中配置 Redirect URI
|
||||
|
||||
将以下值添加到应用设置界面中的 Redirect URI 配置项:
|
||||
|
||||
```bash
|
||||
$(LOGTO_REDIRECT_SCHEME)://$(YOUR_APP_PACKAGE)/callback
|
||||
```
|
||||
|
||||
注意:
|
||||
- `LOGTO_REDIRECT_SCHEME` 应为自定义的反向域名格式的一串字符。
|
||||
- 将上述的 `$(LOGTO_REDIRECT_SCHEME)` 替换成你定义的值。
|
||||
|
||||
例: `io.logto.android://io.logto.sample/callback`
|
||||
|
||||
### 配置 Logto Android SDK
|
||||
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
import io.logto.sdk.android.LogtoClient
|
||||
import io.logto.sdk.android.type.LogtoConfig
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
val logtoConfig = LogtoConfig(
|
||||
endpoint = "<your-logto-endpoint>",
|
||||
appId = "<your-app-id>",
|
||||
scopes = null,
|
||||
resources = null,
|
||||
usingPersistStorage = true,
|
||||
)
|
||||
|
||||
val logtoClient = LogtoClient(logtoConfig, application)
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
<TabItem value="java" label="Java">
|
||||
|
||||
```java
|
||||
import io.logto.sdk.android.LogtoClient;
|
||||
import io.logto.sdk.android.type.LogtoConfig;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private LogtoClient logtoClient;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
LogtoConfig logtoConfig = new LogtoConfig(
|
||||
"<your-logto-endpoint>",
|
||||
"<your-app-id>",
|
||||
null,
|
||||
null,
|
||||
true
|
||||
);
|
||||
|
||||
logtoClient = new LogtoClient(logtoConfig, getApplication());
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
||||
Notes:
|
||||
|
||||
- `<your-logto-endpoint>` 是你运行 Logto 服务所在的地址. 若你的 Logto 服务运行在 `http://localhost:300`,则 `<your-logto-endpoint` 为 `http://localhost:300`
|
||||
- `<your-app-id>` 是你为自己的应用在管理员控制台中所创建的客户端ID.
|
||||
|
||||
|
||||
## 步骤 3: 登录
|
||||
|
||||
|
||||
### 执行登录
|
||||
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
logtoClient.signInWithBrowser(
|
||||
this,
|
||||
"io.logto.android://io.logto.sample/callback",
|
||||
) { logtoException: LogtoException? ->
|
||||
// 后续处理逻辑
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
<TabItem value="java" label="Java">
|
||||
|
||||
```java
|
||||
logtoClient.signInWithBrowser(
|
||||
this,
|
||||
"io.logto.android://io.logto.sample/callback",
|
||||
logtoException -> {
|
||||
// 后续处理逻辑
|
||||
}
|
||||
);
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
||||
### 登录成功后,你可以使用 SDK 提供的一些 API 来实现自己的业务逻辑
|
||||
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
logtoClient.getAccessToken { logtoException: LogtoException?, result: AccessToken? ->
|
||||
// 后续处理逻辑
|
||||
}
|
||||
|
||||
logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenClaims? ->
|
||||
// 后续处理逻辑
|
||||
}
|
||||
|
||||
logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: UserInfoResponse? ->
|
||||
// 后续处理逻辑
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
<TabItem value="java" label="Java">
|
||||
|
||||
```java
|
||||
logtoClient.getAccessToken((logtoException, accessToken) -> {
|
||||
// 后续处理逻辑
|
||||
});
|
||||
|
||||
logtoClient.getIdTokenClaims((logtoException, idTokenClaims) -> {
|
||||
// 后续处理逻辑
|
||||
});
|
||||
|
||||
logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> {
|
||||
// 后续处理逻辑
|
||||
});
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
||||
|
||||
## 步骤 4: 登出
|
||||
|
||||
### 执行登出
|
||||
|
||||
<Tabs>
|
||||
|
||||
<TabItem value="kotlin" label="Kotlin">
|
||||
|
||||
```kotlin
|
||||
logtoClient.signOut { logtoException: LogtoException? ->
|
||||
// 后续处理逻辑
|
||||
}
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
<TabItem value="java" label="Java">
|
||||
|
||||
```java
|
||||
logtoClient.signOut(logtoException -> {
|
||||
// 后续处理逻辑
|
||||
});
|
||||
```
|
||||
|
||||
</TabItem>
|
||||
|
||||
</Tabs>
|
||||
|
||||
注意:
|
||||
|
||||
- 登出操作会清除本地存储的用户相关凭据,即使在登出过程中发生了异常。
|
||||
|
||||
|
||||
## 步骤 5: 延伸阅读
|
||||
|
||||
- 获取用户信息
|
||||
- 配置社会化登录
|
||||
- 访问受保护的 API 资源
|
Loading…
Reference in a new issue