¶ Login by OPPO
¶ Preparatory work
Configuration work:In OPPO open platform (opens new window)) and Authing Console (opens new window) for application to apply for and configuration, please refer to the OPPO access to.
Development reference:see OPPO official documentation (opens new window)。
This feature was added in android guard sdk 1.5.6 version.
¶ Integrate steps
¶ Step 1:Add dependency
- Configure under the
repositories
node of the build.gradle file in the root directory of your project
maven {
url 'https://maven.columbus.heytapmobi.com/repository/maven-public/'
credentials {
username 'cmuser'
password 'c0b08da17ery566c3870fed67789bcb36abc2e32'
}
}
- Add the following dependencies under the build.gradle file
dependencies
node in the app module
dependencies {
implementation 'cn.authing:guard:+'
implementation 'com.heytap.msp.sdk:msp-sdk:1.10.1.2'
implementation 'com.heytap.msp.sdk:msp-oauth-sdk:1.10.1.2'
}
The Guard compileOnly relies OPPO 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.
¶ Step 2:Configuration AndroidManifest
Configure the APPID generated by the MSP open platform under the application node of the AndroidManifest.xml file.
<meta-data
android:name="com.heytap.msp.client.appid"
android:value="xxxxxxxx" />
¶ Step 3:Configuration obfuscation script
Open the obfuscation configuration file progurad-rules.pro for the Android Studio project and add the obfuscation configuration
-ignorewarning
-keepattributes Annotation
-keepattributes Exceptions
-keepattributes InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-keepclassmembers enum * {
public static [] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator ;
}
-keep class * implements java.io.Serializable{;}
-keep class com.bun.miitmdid. {;}
-keeppackagenames com.heytap.msp**
-keep class com.heytap.openid.sdk.HeytapIDSDK{;}
-keep class com.heytap.usercenter.{;}
-keep class com.platform.usercenter.annotation.Keep
-keep @com.platform.usercenter.annotation.Keep class * {;}
-keep interface com.heytap.msp.{;}
-keep class com.heytap.msp.sdk.base.**{;}
-keep class com.heytap.msp.sdk.SdkAgent{;}
-keep class com.heytap.msp.sdk.common.**{;}
-keep class com.heytap.msp.sdk.agent.AccountSdkAgent{;}
-keep class com.heytap.msp.sdk.AccountSdk{;}
-keep class com.heytap.msp.sdk.agent.OAuthSdkAgent{;}
-keep class com.heytap.msp.sdk.OAuthSdk{;}
-keep class com.platform.oms.**{*;}
¶ Step 4:Initialization
Initialize the Guard Android SDK in the Application:
// 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)
SdkAgent.init(this);
¶ Step 5: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 OPPO login function. The login entry is displayed in the social login button list on the built-in login interface of the Guard.
¶ Use the login button
If you use the OPPO login button we provide.
- Add the following code to the layout file:
<cn.authing.guard.social.view.OPPOLoginButton
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" />
- Then handle the event in the code:
OPPOLoginButton 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
}
}
});
¶ Use the login authorization class
If you don't want to use our built-in buttons and want to implement the UI entirely yourself, you can call the OPPO
class authorization function inside the button click event, which integrates the business logic to pull up the OPPO authorization login:
OPPO.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.).
¶ Use the login API
If you want to fully implement the OPPO 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 loginByOppo(String authCode, @NotNull AuthCallback<UserInfo> callback)
param
authCode
OPPO authCode
example
If you only need to get the user information (username
, nickname
, name
, etc.) and idToken
, call:
AuthClient.loginByOppo(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.loginByOppo(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
}
}
});