By integrating the HUAWEI ID sign-in capability built based on OAuth 2.0 and OpenID Connect, an atomic service can easily obtain the identity information of HUAWEI ID users to quickly establish its own user system.
When a user opens an atomic service, the service obtains the user's identity information (UnionID/OpenID) and signs the user in silently, without asking the user to sign in or sign up by tapping the relevant button.
If you want to link a user with a registered account to synchronize historical data assets, you can request the mobile number from the user.
Preparations
Call has.login to obtain the OpenID. You can also pass the code value returned by the has.login API when calling the server-side API for obtaining a user-level credential, and then obtain the OpenID by calling the API for parsing a credential. code returned by has.login is the authorization code required during a call of the API for obtaining a user-level credential.
has.login({
success: (res) => {
console.info('login success', res);
},
fail: (err) => {
console.error('login fail', err);
},
complete: (res) => {
console.info('login complete', res);
}
}); When your atomic service needs to obtain a user's mobile number, call button component and set open-type to getPhoneNumber to guide the user to complete mobile number authorization.
Ensure that your atomic service meets the mobile number usage requirements specified in the Regulations on the Scope of Necessary Personal Information for Common Types of Mobile Internet Applications. (Huawei does not assume any responsibility for the content on third-party websites.)
Procedure:
Set open-type of the button component to getPhoneNumber. After the user taps the button and grants authorization, obtain the callback information via the bindgetphonenumber event.
Pass the dynamic token code in the bindgetphonenumber event callback to your backend, call the server-side API to consume this code and obtain the user-level credential, and then call the API for obtaining user information to obtain the user's mobile number. Each code value is valid for 5 minutes and can be consumed only once. The dynamic token code in bindgetphonenumber is the authorization code required during a call of the API for obtaining a user-level credential.
code returned by getPhoneNumber is different from that returned by has.login. They cannot be used interchangeably.
Sample code
<!--HXML file--> <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">"get phonenumber"</button>
// JavaScript file
Page({
getPhoneNumber(e) {
// Dynamic token
console.info(e.detail.code);
// Callback information (returned for both success and failure)
console.info(e.detail.errMsg);
// Error code (returned upon failure)
console.error(e.detail.errno);
}
}); When your atomic service needs to obtain the invoice title of a user, call has.chooseInvoiceTitle to open the invoice title selection screen for the user to select or manage the invoice titles.
When your atomic service needs to obtain a user's shipping address, call has.chooseAddress to guide the user to add or select a shipping address.
has.chooseAddress({
success: (res) => {
console.info('chooseAddress success,res', res);
},
fail: (err) => {
console.error('chooseAddress fail', err);
},
complete: (res) => {
console.info('chooseAddress complete', res);
}
});