当前位置:网站首页>Rapid integration of authentication services - harmonyos platform
Rapid integration of authentication services - harmonyos platform
2022-07-07 08:35:00 【Huawei Developer Forum】
We can integrate authentication services in applications SDK To easily and quickly launch registration to users 、 Login and other related functions . Here's how to use HarmonyOS Platform integration AGC Certification services .
Integrated content
adopt harmony sdk Plug in integration AGC The validation service , stay AGC Start the authentication service on , And add two ways of mobile phone verification and email verification , At the same time, end-to-side integration SDK Called by AGC Send verification code to mobile phone or email , Enter the verification code and password to complete user registration .
Integration preparation
- install DevEco Studio development tool , And be familiar with Harmony Application development process .
- AGC Create projects and open services .
a) stay AGC establish Harmony Apply and open the verification service .
Enable mobile phone and email authentication
3、DevEco Studio Created in Harmony engineering
a) open IDE Tools , choice File-New-New project, choice Java After the language compiles the template , Click on Next Enter the create project configuration interface .
Pay attention to the application (Application), Package name and AGC The application package name created is the same .
b) take agconnect-services.json Copy the file to the project's entry Module directory
c) To configure Maven Warehouse address and AGC Plug-in address .
open harmony Project level build.gradle file .
stay allprojects ->repositories It's equipped with maven Warehouse address .
stay buildscript->repositories Middle configuration maven Warehouse address .
stay buildscript->dependencies Middle configuration AppGallery Connect Plug-in address .
The test environment needs to configure the internal private warehouse address , Formal environment requires configuration of external formal warehouse .
buildscript { repositories { maven { url 'https://repo.huaweicloud.com/repository/maven/' } maven { url 'https://developer.huawei.com/repo/' } maven { url 'http://language.cloudartifact.dgg.dragon.tools.huawei.com/artifactory/product_maven' } maven { url 'http://szg1.artifactory.inhuawei.com/artifactory/sz-maven-public/' } jcenter() } dependencies { classpath 'com.huawei.ohos:hap:2.4.4.2' classpath 'com.huawei.ohos:decctest:1.2.4.0' classpath 'com.huawei.agconnect:agcp-harmony:1.2.0.200' }}allprojects { repositories { maven { url 'https://repo.huaweicloud.com/repository/maven/' } maven { url 'https://developer.huawei.com/repo/' } maven { url 'http://language.cloudartifact.dgg.dragon.tools.huawei.com/artifactory/product_maven' } maven { url 'http://szg1.artifactory.inhuawei.com/artifactory/sz-maven-public/' } jcenter() }}
d) Add compilation dependencies and integrations SDK.
open harmony Project application level (entry) Of build.gradle file , Add plug-in address .
apply plugin: 'com.huawei.ohos.hap'apply plugin: 'com.huawei.ohos.decctest'apply plugin: 'com.huawei.agconnect'
stay “dependencies” Add... To agconnect-core Compilation dependencies and crash SDK.
dependencies { implementation fileTree(dir: 'libs', include: ['*.jar', '*.har']) implementation 'com.huawei.agconnect:agconnect-auth-harmony:1.2.0.1-snapshot' implementation 'com.huawei.agconnect:agconnect-tasks-harmony:1.2.0.1-snapshot'}
e)entry (hap) Add configuration information to the module
enrty Modular config.json Of documents module/abilities in , Add the following configuration , Pay attention to the replacement of package name .
{ "permissions": [ "com.huawei.agconnect.core.DataAbilityShellProvider.PROVIDER" ], "name": "com.huawei.agconnect.core.provider.AGConnectInitializeAbility", "type": "data", "uri": "dataability://com.huawei.agconnect.auth.AGConnectInitializeAbility"}
f) Synchronize project configuration
4、 Configure signature information
Harmony Install the application to the real machine for testing , Signature information needs to be configured , There are two ways of manual signature and automatic signature , For details, please refer to : Configure signature information . It is recommended to give priority to automatic signature , If the automatic signature fails , You can choose to sign manually . Here we use automatic signature :
a) Connect real equipment , Make sure DevEco Studio Connected to the real device , After the real machine is successfully connected, it is shown in the following figure
b) Get into File > Project Structure > Project > Signing Configs Interface , Click on “Sign In” Button to log in , choice Debug, Check Automatically generate signing, Click on try again You can automatically sign . Automatically generate the key required for signature (.p12)、 digital certificate (.cer) and Profile file (.p7b) Will be saved to the user user In the catalog .ohos\config Under the table of contents .
After setting the signature information , Click on OK Preservation , Then you can do it under the project build.gradle View signature configuration information in .
Sign in AGC, Get into “ Users and access ”-“ Certificate management ”, Click Add certificate , Upload the previously generated .csr file , Type selection commissioning certificate
After completing the signature configuration, you can click Install to apply to the debugging device .
Function realization
Mobile authentication login layout design
Refer to the layout above , Enter the country code in the four text boxes 、 cell-phone number 、 password 、 Verification Code , Click to verify and receive the verification code SMS , Enter verification code , Click login , Complete registration and login The layout code is as follows :
<?xml version="1.0" encoding="utf-8"?><DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent"> <TextField ohos:id="$+id:countryCode" ohos:height="match_content" ohos:width="150vp" ohos:hint="Your countryCode" ohos:text_input_type="pattern_number" ohos:text_size="50" ohos:top_margin="10vp" /> <TextField ohos:id="$+id:phoneNumber" ohos:height="match_content" ohos:width="150vp" ohos:hint="Your phone" ohos:text_input_type="pattern_number" ohos:text_size="50" ohos:bottom_margin="10vp" /> <TextField ohos:id="$+id:password" ohos:height="match_content" ohos:width="150vp" ohos:hint="Password" ohos:text_input_type="pattern_password" ohos:text_size="50" ohos:bottom_margin="10vp" /> <TextField ohos:id="$+id:verifyCode" ohos:height="match_content" ohos:width="150vp" ohos:hint="verifyCode" ohos:text_input_type="pattern_number" ohos:text_size="50" ohos:bottom_margin="10vp" /> <DirectionalLayout ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal"> <Button ohos:id="$+id:Verify" ohos:height="match_content" ohos:width="100vp" ohos:background_element="$graphic:background_ability_main" ohos:left_margin="15vp" ohos:text="Verify" ohos:text_size="16fp"/> <Button ohos:id="$+id:Login" ohos:height="match_content" ohos:width="100vp" ohos:background_element="$graphic:background_ability_main" ohos:left_margin="15vp" ohos:text="Login" ohos:text_size="16fp"/> </DirectionalLayout></DirectionalLayout>
Authentication and login :
Enter the country code 、 cell-phone number 、 Then click Verify, Return the verification code sending success log , The mobile phone receives the verification code , Enter the password and verification code and click Login Return the login success log :
The page jumps to the login success page :
The authentication and login codes are as follows :
private void verify() { String countryCode = getCountryCode(); String phoneNumber = getPhoneNumber(); int action = VerifyCodeSettings.ACTION_REGISTER_LOGIN; VerifyCodeSettings settings = VerifyCodeSettings.newBuilder() .action(action) .sendInterval(30) .build(); HarmonyTask<VerifyCodeResult> task = AGConnectAuth.getInstance() .requestVerifyCode(countryCode, phoneNumber, settings); task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() { @Override public void onSuccess(VerifyCodeResult verifyCodeResult) { log("verifyPhoneCode success"); } }).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() { @Override public void onFailure(Exception e) { log("verifyPhoneCode fail:" + e); } });}private void login() { String countryCode = getCountryCode(); String phoneNumber = getPhoneNumber(); String password = getPassword(); String verifyCode = getVerify(); AGConnectAuthCredential phoneAuthCredential = PhoneAuthProvider.credentialWithVerifyCode(countryCode, phoneNumber, password, verifyCode); /* signIn */ HarmonyTask<SignInResult> task = AGConnectAuth.getInstance().signIn(phoneAuthCredential); task.addOnSuccessListener(new OnSuccessListener<SignInResult>() { @Override public void onSuccess(SignInResult signInResult) { log("signIn success"); showUser(signInResult.getUser()); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { log("signInWithCode fail:" + e); } });}
2、 Mailbox verification login layout
Refer to the layout above , Enter the mailbox number in the three text boxes 、 password 、 Verification Code , Click to verify and receive the verification code SMS , Enter verification code , Click login , Complete registration and login , The layout code is as follows :
<?xml version="1.0" encoding="utf-8"?><DirectionalLayout xmlns:ohos="http://schemas.huawei.com/res/ohos" ohos:width="match_parent" ohos:height="match_parent"> <TextField ohos:id="$+id:phoneNumber" ohos:height="match_content" ohos:width="150vp" ohos:hint="Your phone" ohos:text_input_type="pattern_number" ohos:text_size="50" ohos:bottom_margin="10vp" /> <TextField ohos:id="$+id:password" ohos:height="match_content" ohos:width="150vp" ohos:hint="Password" ohos:text_input_type="pattern_password" ohos:text_size="50" ohos:bottom_margin="10vp" /> <TextField ohos:id="$+id:verifyCode" ohos:height="match_content" ohos:width="150vp" ohos:hint="verifyCode" ohos:text_input_type="pattern_number" ohos:text_size="50" ohos:bottom_margin="10vp" /> <DirectionalLayout ohos:height="match_content" ohos:width="match_parent" ohos:orientation="horizontal"> <Button ohos:id="$+id:Verify" ohos:height="match_content" ohos:width="100vp" ohos:background_element="$graphic:background_ability_main" ohos:left_margin="15vp" ohos:text="Verify" ohos:text_size="16fp"/> <Button ohos:id="$+id:Login" ohos:height="match_content" ohos:width="100vp" ohos:background_element="$graphic:background_ability_main" ohos:left_margin="15vp" ohos:text="Login" ohos:text_size="16fp"/> </DirectionalLayout></DirectionalLayout>
Enter the email number and click Verify, Return the verification code sending success log , Email received verification code , Enter the password and verification code and click Login Return the login success log :
The page jumps to the login success page :
The email verification and login codes are as follows :
private void verify() { //String countryCode = getCountryCode(); String emailStr = getEmailStr(); int action = VerifyCodeSettings.ACTION_REGISTER_LOGIN; VerifyCodeSettings settings = VerifyCodeSettings.newBuilder() .action(action) .sendInterval(30) .build(); HarmonyTask<VerifyCodeResult> task = AGConnectAuth.getInstance() .requestVerifyCode(emailStr, settings); task.addOnSuccessListener(TaskExecutors.uiThread(), new OnSuccessListener<VerifyCodeResult>() { @Override public void onSuccess(VerifyCodeResult verifyCodeResult) { log("verifyEmailCode success"); } }).addOnFailureListener(TaskExecutors.uiThread(), new OnFailureListener() { @Override public void onFailure(Exception e) { log("verifyEmailCode fail:" + e); } });}private void login() { //String countryCode = getCountryCode(); String emailStr = getEmailStr(); String password = getPassword(); String verifyCode = getVerify(); AGConnectAuthCredential emailAuthCredential = EmailAuthProvider.credentialWithVerifyCode(emailStr, password, verifyCode); /* signIn */ HarmonyTask<SignInResult> task = AGConnectAuth.getInstance().signIn(emailAuthCredential); task.addOnSuccessListener(new OnSuccessListener<SignInResult>() { @Override public void onSuccess(SignInResult signInResult) { log("signIn success"); showUser(signInResult.getUser()); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(Exception e) { log("signInWithCode fail:" + e); } });}
A functional test
After landing successfully , open AGC The validation service , Query data in the background :
Users who register with mobile phones and e-mail all display account information and authentication times in the background , The number of verifications is the same as the number of requests ;
stay AGC Delete account on , If you log in again with the same user name and password, you will report that the user name or password is wrong
After disabling the account, login will report that the account has been disabled .
For more technical articles , Welcome to visit https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh
边栏推荐
- Teach you how to select PCB board by hand (II)
- Transformation function map and flatmap in kotlin
- PVTV2--Pyramid Vision TransformerV2学习笔记
- Lua programming learning notes
- Opencv learning notes 1 -- several methods of reading images
- Detailed explanation of apply, also, let, run functions and principle analysis of internal source code in kotlin
- Thirteen forms of lambda in kotlin
- Practice of combining rook CEPH and rainbow, a cloud native storage solution
- Standard function let and generic extension function in kotlin
- The single value view in Splunk uses to replace numeric values with text
猜你喜欢
如何在HarmonyOS应用中集成App Linking服务
Opencv learning note 5 - gradient calculation / edge detection
Obsidan之数学公式的输入
登山小分队(dfs)
Opencv learning note 4 - expansion / corrosion / open operation / close operation
SSM integration
BiSeNet的特點
Analyzing the influence of robot science and technology development concept on Social Research
23 Chengdu instrument customization undertaking_ Discussion on automatic wiring method of PCB in Protel DXP
Iptables' state module (FTP service exercise)
随机推荐
IELTS review progress and method use [daily revision]
[Yu Yue education] basic reference materials of electrical and electronic technology of Nanjing Institute of information technology
Function extension, attribute extension and non empty type extension in kotlin
Merge sort and non comparison sort
Learn how to compile basic components of rainbow from the source code
In go language, function is a type
POJ - 3784 running medium
Ebpf cilium practice (2) - underlying network observability
Give full play to the wide practicality of maker education space
使用AGC重签名服务前后渠道号信息异常分析
如何在HarmonyOS应用中集成App Linking服务
快速集成认证服务-HarmonyOS平台
MySQL introduction - crud Foundation (establishment of the prototype of the idea of adding, deleting, changing and searching)
Basic data types and string types are converted to each other
Improve the delivery efficiency of enterprise products (1) -- one click installation and upgrade of enterprise applications
Download and install orcale database11.2.0.4
Pvtv2--pyramid vision transformer V2 learning notes
2-3查找樹
Opencv learning note 4 - expansion / corrosion / open operation / close operation
PVTV2--Pyramid Vision TransformerV2学习笔记