¶ 支付宝登录
更新时间: 2022-10-11 18:51:40
¶ 准备工作
在支付宝开放平台 (opens new window)及 Authing Console 控制台 (opens new window)进行配置,请参阅支付宝。
¶ 集成支付宝登录步骤
¶ 步骤 1:下载支付宝 SDK
在这个页面下载 支付宝 Android SDK (opens new window)
支付宝将 Android、iOS 的 SDK 和 Demo 打包到一个 zip 包里面,找到里面的安卓 SDK,拷贝到 app 的 libs 目录
¶ 步骤 2:添加依赖
implementation 'cn.authing:guard:+'
implementation files('libs/alipaysdk.aar')
Guard 只是 compileOnly 依赖支付宝 SDK,这样可以让 App 按需引入,防止 Guard aar 包随着支持的第三方登录增加而越来越大。所以每增加一个第三方身份源,都需要 App 手动加上该身份源的依赖
¶ 步骤 3:初始化 Guard Android SDK
在应用启动的时候初始化:
// context is application or initial activity
// ”AUTHING_APP_ID“ is obtained from the Authing console
Authing.init(context, "AUTHING_APP_ID");
通过以上步骤即可简单快速的通过 Authing 管理控制台配置后自动获取飞书身份源,登录入口会在 Guard 内置登录界面的社会化登录按钮列表中体现
- 接下来,如果使用我们提供的支付宝登录按钮,则在布局文件里面加上(当然也可以用代码初始化):
<cn.authing.guard.AlipayLoginButton
android:id="@+id/btn_alipay_login"
android:layout_width="44dp"
android:layout_height="44dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
然后在 java 代码里面处理事件:
AlipayLoginButton button = findViewById(R.id.btn_alipay_login);
button.setOnLoginListener((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息
} else {
// 登录失败
}
});
- 如果不想使用我们内置的按钮,则可以在自己按钮的点击事件里面调用:
Alipay alipay = new Alipy();
alipay.login(appContext, ((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息
} else {
// 登录失败
}
}));
- 如果想完全自己实现支付宝登录,拿到授权码后,可以调用下面 API 换取用户信息:
public static void loginByAlipay(String authCode, @NotNull AuthCallback<UserInfo> callback)
参数
authCode支付宝授权码
示例
AuthClient.loginByAlipay(authCode, (code, message, userInfo)->{
if (code == 200) {
// userInfo:用户信息
}
});