¶ 企业微信(代开发模式)登录
更新时间: 2022-10-17 15:44:17
¶ 准备工作
在企业微信服务商后台 (opens new window)及 Authing Console 控制台 (opens new window) 进行配置,请参阅企业微信移动端(代开发模式)。
¶ 集成企业微信(代开发模式)登录步骤
¶ 步骤 1:添加依赖
- 下载企业微信 SDK (opens new window);
- 将下载的 sdk jar 文件拷贝到工程的 libs 目录下;
- 在工程的 build.gradle 文件中引入该 jar,引入相关依赖项。
implementation 'cn.authing:guard:+'
implementation files('libs/lib_wwapi-2.0.12.11.aar')
Guard 只是 compileOnly 依赖企业微信,这样可以让 App 按需引入,防止 Guard aar 包随着支持的第三方登录增加而越来越大。所以每增加一个第三方身份源,都需要 App 手动加上该身份源的依赖
如果需要混淆代码,为了保证sdk的正常使用,需要在proguard.cfg加上下面两行配置:
-keep class com.tencent.wework.api.** {
*;
}
¶ 步骤 2:初始化 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.WeComLoginButton
android:id="@+id/btn_wecom_login"
android:layout_width="44dp"
android:layout_height="44dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
然后在 java 代码里面处理事件:
WeComLoginButton button = findViewById(R.id.btn_wecom_login);
button.setOnLoginListener((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息,里面有 accessToken
} else {
// 登录失败
}
});
- 如果不想使用我们内置的按钮,则可以在自己按钮的点击事件里面调用 Authing 企业微信登录 API:
WeCom weCom = new WeCom();
weCom.login(appContext, ((ok, data) -> {
if (ok) {
// 登录成功,data 是用户信息,里面有 accessToken
} else {
// 登录失败
}
}));
- 如果想完全自己实现企业微信登录,拿到授权码后,可以调用下面 API 换取用户信息:
public static void loginByWecom(String authCode, @NotNull AuthCallback<UserInfo> callback)
参数
authCode企业微信授权码
示例
AuthClient.loginByWecom(authCode, (code, message, userInfo)->{
if (code == 200) {
// userInfo:用户信息
}
});