用户可借助此项目快速集成华为账号服务REST API,实现凭证管理、用户信息获取、实名认证、订阅用户信息变更等功能,提供完整的调用示例和工具类支持。
| Files | Last commit | Last update |
|---|---|---|
| 16 days ago | ||
| 16 days ago | ||
| 4 months ago | ||
| 5 months ago | ||
| 4 months ago | ||
| 4 months ago | ||
| 5 months ago |
Account Kit - Server-Side API Sample
Read this README to learn how to integrate Account Kit REST APIs.
Overview
This sample illustrates how to call APIs for credential management, user information obtaining, identity verification, subscription, and user information change processing, helping you quickly integrate Account Kit.
Environment Dependencies
- JDK: 1.8 or later
- Maven: 3.6 or later
Project Directory
├── src/main/java/com/huawei/accountkit/
│ ├── credential/ # Credential management module.
│ │ ├── req/ # Request parameter class.
│ │ ├── resp/ # Response result class.
│ │ ├── GetAppCredentialService.java # Obtain an app-level credential.
│ │ ├── GetTokenInfoService.java # Parse a credential.
│ │ ├── GetUserCredentialService.java # Obtain a user-level credential.
│ │ ├── RefreshUserCredentialService.java # Refresh a user-level credential.
│ │ └── TokenRevokeService.java # Revoke authorization for a user-level credential.
│ ├── discontinuedapi/ # API module no longer maintained.
│ │ └── userinfo/ # User information API no longer maintained.
│ │ ├── req/ # Request parameter class.
│ │ ├── resp/ # Response result class.
│ │ └── GetQuickLoginMobileByAccessTokenService.java # Obtain the UnionID/OpenID and mobile number bound to a HUAWEI ID with one-tap sign-in (no longer maintained).
│ ├── extra/ # Extended capability module.
│ │ ├── req/ # Request parameter class.
│ │ ├── resp/ # Response result class.
│ │ ├── GetGroupUnionIdService.java # Obtain a GroupUnionID using an OpenID or UnionID.
│ │ ├── GetGroupUnionIdByCodeService.java # Obtain a GroupUnionID using an authorization code.
│ │ ├── GetJwtPublicKeyService.java # Obtain the JWT public key for ID token verification.
│ │ ├── GetOpenIdConnectConfigService.java # Obtain OpenID Connect public configurations.
│ │ ├── OpenIdToUnionIdService.java # Obtain the UnionID through the OpenID.
│ │ └── VerifyIdTokenService.java # Verify the validty of an ID token.
│ ├── realname/ # Identity verification module.
│ │ ├── req/ # Request parameter class.
│ │ ├── resp/ # Response result class.
│ │ ├── GetDetailInfoService.java # Obtain real-name information.
│ │ ├── GetUserAgeRangeService.java # Obtain the age range of a real-name user.
│ │ └── VerifyRealNameService.java # Verify real-name information.
│ ├── subscribe/ # Subscription module.
│ │ └── HwUserInfoChangeHandler.java # Subscribe to and process user information changes.
│ ├── userinfo/ # User information module.
│ │ ├── req/ # Request parameter class.
│ │ ├── resp/ # Response result class.
│ │ ├── GetAvatarAndNickNameByAccessTokenService.java # Obtain the avatar and nickname of a HUAWEI ID user.
│ │ ├── GetMobileByAccessTokenService.java # Obtain the mobile number of a HUAWEI ID user.
│ │ ├── GetQuickLoginMobileByCodeService.java # Obtain the UnionID/OpenID and mobile number bound to a HUAWEI ID with one-tap sign-in.
│ │ └── GetUserRiskLevelService.java # Obtain a user's risk level.
│ └── util/ # Utility class module.
│ └── CallUtils.java # HTTP call utility class.
├── src/test/java/com/huawei/accountkit/
│ ├── credential/ # Credential management module testing.
│ ├── userinfo/ # User information module testing.
│ ├── extra/ # Extended capability module testing.
│ ├── realname/ # Identity verification module testing.
│ └── discontinuedapi/ # Testing of the API module no longer maintained.
How to Use
1. API Call Credentials
Main Functions
- GetAppCredentialService: Obtain an app-level credential.
- GetUserCredentialService: Obtain a user-level credential.
- RefreshUserCredentialService: Refresh a user-level credential
- GetTokenInfoService: Parse a credential.
- TokenRevokeService: Revoke authorization for a user-level credential.
Example
// Obtain a user-level credential.
GetUserCredentialReq req = new GetUserCredentialReq();
// Authorization code obtained through the app.
req.setCode("your_authorization_code");
req.setClientId("your_client_id");
req.setClientSecret("your_client_secret");
// Pass the authorization code.
req.setGrantType("authorization_code");
// Call the API.
GetUserCredentialResp resp = GetUserCredential.call(req);
String accessToken = resp.getAccessToken();
2. Obtaining User Information
Main Functions
- GetAvatarAndNickNameByAccessTokenService: Obtain the avatar and nickname of a HUAWEI ID user.
- GetMobileByAccessTokenService: Obtain the mobile number of a HUAWEI ID user.
- GetQuickLoginMobileByCodeService: Obtain the UnionID/OpenID and mobile number bound to a HUAWEI ID with one-tap sign-in.
- GetUserRiskLevelService: Obtain a user's risk level.
Example
// Obtain the avatar and nickname of a HUAWEI ID user.
GetAvatarAndNickNameByAccessTokenReq req = new GetAvatarAndNickNameByAccessTokenReq();
req.setAccessToken("your_access_token");
// 1 indicates that the nickname will be obtained.
req.setGetNickName(1);
GetAvatarAndNickNameByAccessTokenResp resp = GetAvatarAndNickNameByAccessToken.call(req);
String headPictureURL = resp.getHeadPictureURL();
String displayName = resp.getDisplayName();
3. Identity Verification
Main Functions
- GetDetailInfoService: Obtain real-name information.
- GetUserAgeRangeService: Obtain the age group of a real-name user.
- VerifyRealNameService: Verify real-name information.
Example
// Obtain the age group of a real-name user.
GetUserAgeRangeReq req = new GetUserAgeRangeReq();
req.setAccessToken("your_access_token");
req.setClientId("your_client_id");
GetUserAgeRangeResp resp = GetUserAgeRangeService.call(req);
4. Extended Capabilities
Main Functions
- GetJwtPublicKeyService: Obtain the JWT public key for ID token verification.
- VerifyIdTokenService: Verify the validity of an ID token.
- GetGroupUnionIdService: Obtain a GroupUnionID through an OpenID or UnionID.
- GetGroupUnionIdByCodeService: Obtain a GroupUnionID through an authorization code.
- OpenIdToUnionIdService: Obtain a UnionID through an OpenID.
- GetOpenIdConnectConfigService: Obtain OpenID Connect public configurations.
Example
// Obtain the JWT public key and verify the ID token.
// 1. Obtain the JWT public key.
GetJwtPublicKeyResp publicKeyResp = GetJwtPublicKey.call();
// 2. Verify the ID token.
String idToken = "your_id_token";
VerifyIdTokenResp verifyResp = VerifyIdToken.call(idToken);
5. Subscribing to and Processing User Information Changes
Example
// <token> in Authorization: Bearer <token> of the request header.
String token = "token";
// Client ID
String clientId = "your_client_id";
Optional<Jwt<?, ?>> jwtOptional = new HwUserInfoChangeHandler().validateSecurityEventToken(token, clientId);
if (!jwtOptional.isPresent()) {
// Signature verification failed.
log.error("verify sign failed");
} else {
// Signature verification succeeded.
log.info("verify sign success");
}
6. Utility Class Module
It provides core utility classes for HTTP calling and function support.
CallUtils
-
Main functions:
- HTTP client connection pooling management
- Unified exception processing mechanism
- JSON data serialization/deserialization
-
Key features:
- SSL/TLS support: TLS 1.2
- Exception handling: provides specialized, tailored error-handling mechanisms mapped to different API types.
- JSON processing: powered by FastJSON2 for efficient JSON parsing.
Security Suggestions
- Sensitive information protection: Critical credentials (such as the client secret) must be stored using secure encryption methods and should never be hardcoded directly into the source code.
- Token management: Ensure safe custody and secure storage of users' access tokens and refresh tokens.
References
Open Source License
The sample code for Account Kit server-side APIs is licensed under Apache License 2.0.