¶ Login by DingTalk
¶ Preparatory work
Configure in DingTalk open platform (opens new window) and Authing Console (opens new window),See Preparing for DingTalk、DingTalk document (opens new window)。
This feature was added in android guard sdk 1.5.1 version.
¶ Integrate steps
¶ Step 1:Add dependency
implementation 'cn.authing:guard:+'
implementation 'com.alibaba.android:ddopenauth:1.5.0.8'
implementation 'com.alibaba.android:ddsharesdk:1.1.0'
The Guard compileOnly relies on DingTalk sdk, which allows apps to import on demand, preventing the Guard aar package from getting bigger as more third party logins are supported. Therefore, every time a third-party identity source is added, the App needs to manually add the dependency of the identity source.
Precautions
If you need to obfuscate code, add the following configuration to proguard.cfg to ensure proper use of the sdk:
-keep class com.android.dingtalk.openauth.**{*;}
¶ Step 2:Initialization
To initialize Guard Android SDK when the application starts:
// context is application or initial activity
// ”AUTHING_APP_ID“ is obtained from the Authing console
Authing.init(context, "AUTHING_APP_ID");
Authing.setAuthProtocol(Authing.AuthProtocol.EOIDC)
¶ Step 3:Create DDAuthActivity
- Create a new
ddauth
folder in the directory corresponding to the package name and addDDAuthActivity
. Suppose your package name iscom.example.myapp
and its contents only need to inherit from our implementation class:
package com.example.myapp.ddauth;
import cn.authing.guard.social.callback.ddauth.DDCallBackActivity;
public class DDAuthActivity extends DDCallBackActivity {
}
Add a declaration to the project
AndroidManifest.xml
:<activity android:name=".ddauth.DDAuthActivity" android:exported="true"/>
¶ Step 4:Use in different scenarios
¶ Use in different scenarios
Start the managed page where login authentication is required:
// this is the activity context
AuthFlow.start(this);
By following the preceding steps, you can quickly and easily configure the Authing management console to automatically have the DingTalk login function. The login entry is displayed in the social login button list on the built-in login interface of the Guard.
¶ Use the DingTalk sign In button
If you use the DingTalk login button we provide.
1. Add the following code to the layout file:
<cn.authing.guard.social.view.DingTalkLoginButton
android:id="@+id/btn_login"
android:background="@drawable/authing_button_background"
android:textColor="@color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
2. Then handle the event in the code:
DingTalkLoginButton button = findViewById(R.id.btn_login);
button.setOnLoginListener(new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info.
} else {
// login fail
}
}
});
¶ Log in to the authorization class using DingTalk
If you don't want to use our built-in buttons and want to implement the UI entirely yourself, you can call the
DingTalk
class authorization function inside the button click event, which integrates the business logic to pull up the DingTalk authorization login:
DingTalk.getInstance().login(appContext, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info.
} else {
// login fail
}
}
});
data
contains idToken
and user information (user name
, nickname
, name
, etc.).
¶ Log in to the API using DingTalk
If you want to fully implement the DingTalk login UI and obtain the authorization code logic yourself, after obtaining the authorization code, you can call the following API in exchange for user information:
public static void loginByDingTalk(String authCode, @NotNull AuthCallback<UserInfo> callback)
param
authCode
DingTalk token
example
If you only need to get the user information (username
, nickname
, name
, etc.) and idToken
, call:
AuthClient.loginByDingTalk(authCode, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// login success, data is user info, contains idToken.
} else {
// login fail
}
}
});
If you only need to get the user information (username
, nickname
, name
, etc.) and idToken
、accessToken
和 refreshToken
,call:
OIDCClient oidcClient = new OIDCClient();
oidcClient.loginByDingTalk(authCode, new AuthCallback<UserInfo>() {
@Override
public void call(int code, String message, UserInfo data) {
if (code == 200) {
// ogin success, data is user info, contains idToken、accessToken and refreshToken.
} else {
// login fail
}
}
});