Interface Reference documentation:
Ai.baidu.com/ai-doc/FACE…
This paper introduces the operation flow of real name authentication scheme.
This solution is used to implement user real-name authentication in service scenarios such as wechat mini programs, public accounts, and H5. If you use android or IOS apps, you are recommended to use the real-name authentication scheme on the app side
Ai.baidu.com/ai-doc/FACE…
Access sequence diagram
Access to the process
First, to create a Baidu cloud account, face recognition in the product service.
Two, refer to face real name authentication >>> Scheme Overview >>> H5 real name authentication, first create an application, then create a scheme.
3. Create an application and get the AppID, API Key and Secret Key; Create a scheme to get the scheme ID
4. Obtain the Token. Initiate an authentication request through the H5 Obtain Token interface to obtain verify_token information
The access_token is first generated using the API Key and Secret Key obtained in step 3
Generation method:
Send a request to the authorization service address (POST is recommended)
Aip.baidubce.com/oauth/2.0/t…
Grant_type: mandatory parameter, which is fixed to client_credentials.
Client_id: mandatory parameter, application API Key.
Client_secret: mandatory parameter, the Secret Key of the application.
· Example of access_token via POST request URL:
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Va5yQRHlA4Fq5eR3LT0vuXV4&client_secret= 0rDSjzQ20XUj5itV6WRtznPQSzr5pVw2&Copy the code
· Obtaining access_token through Java Example code:
package com.baidu.ai.aip.auth; import org.json.JSONObject; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import java.util.Map; /** * Obtain token class */public class AuthService {/** * obtain permission token * @return Return example: * {* "access_token": "24.460 da4889caad24cccdb1fea17221975. 2592000.1491995545.282335-1234567," * "expires_in" : 2592000 *} */ public static String getAuth() {public static String getAuth() {public static String getAuth(); // The Secret Key obtained from the official website is updated to the String clientSecret = "Baidu cloud application Secret Key"; return getAuth(clientId, clientSecret); } /** * Get the API access token * This token has a certain validity period and needs to be managed by itself, * @param AK - API Key obtained from Baidu cloud * @param sk - Securet Key obtained from Baidu cloud * @return assess_token Example: "*" 24.460 da4889caad24cccdb1fea17221975 2592000.1491995545.282335-1234567 * / public static String getAuth (String ak, String sk) {/ / access token address String authHost = "https://aip.baidubce.com/oauth/2.0/token?"; String getAccessTokenUrl = authHost // 1. Grant_type is a fixed parameter + "grant_type=client_credentials" // 2. API Key + "&client_id=" + ak // 3. Secret Key + "&client_secret=" + sk; try { URL realUrl = new URL(getAccessTokenUrl); HttpURLConnection connection = (HttpURLConnection) realurl.openConnection (); connection.setRequestMethod("GET"); connection.connect(); / / get all the response header field Map < String, a List < String > > Map = connection. GetHeaderFields (); For (String key: map.keyset ()) {system.err.println (key + "-- >" + map.get(key)); } // Define the BufferedReader input stream to read the response to the URL BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String result = ""; String line; while ((line = in.readLine()) ! = null) { result += line; } /** * Return result example */ system.err. Println ("result:" + result); JSONObject jsonObject = new JSONObject(result); String access_token = jsonObject.getString("access_token"); return access_token; } catch (Exception e) {system.err.printf (" Failed to obtain token!" ); e.printStackTrace(System.err); } return null; } public static void main(String[] args) { System.out.println("Access_token: " + getAuth()); }}Copy the code
· Run result: Access_token is obtained
Obtain the Verify_token using the access_token generated above
· We use Postman to send the POST request URL:
https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=24.7f478f1f95d581d6f2f4006e0 F904a95 2592000.1611283726.282336-23149109Copy the code
The information in the URL is as follows:
Access_token: Enter the access_token generated above
(2) Place the request parameter plan_id in the Body, which is the scheme ID value obtained in step 3
Header: application/json
5. Jump to H5 URL for real-name authentication
Use the verify_token information returned in the previous step to request the authentication H5 page and perform client flow operations.
· Authentication URL:
https://brain.baidu.com/face/print/?token=xxx&successUrl=https://xxx&failedUrl=https://xxx
Copy the code
The information in the URL is as follows:
(1) Token: Enter verify_token.
SuccessUrl: The url to which the request was successfully redirected must be prefixed with HTTP or HTTPS
(3) failedUrl: indicates the url to which the request fails. The url must be prefixed with HTTP or HTTPS
After passing the authentication, we can use verify_token and access_token generated in the fourth step to obtain authentication results and data and return user authentication information
6. Interface for querying authentication results
· Send POST request URL using Postman:
https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token=24.7f478f1f95d581d6f2f4006e0f904a95.2592000.1611283726.282336-23149109
Copy the code
The information in the URL is as follows:
(1) Access_token: Enter the access_token generated in Step 4
(2) Place the request parameter verify_token in the Body, that is, the verify_token obtained in step 4
Header: application/json
{ "verify_token" : "cIupeyP51sn28XzxGVTfYqoN"}
Copy the code
Returns the parameter
· Return results
field
Will choose
type
instructions
success
is
boolean
Returns information about whether the request was successful. Return true if the request succeeds; If the request fails, fault is returned
result
is
array
Request the results
+idcard_ocr_result
no
array
Return Collected user face information This parameter is returned when the real-name face authentication console is set to use OCR recognition
++address
no
string
address
++birthday
no
string
birthday
++name
no
string
The name
++id_card_number
no
string
Id number
++gender
no
string
gender
++nation
no
string
national
++expire_time
no
string
Expiry date of ID card
++issue_authority
no
string
Identity card issuing authority
++issue_time
no
string
Id Card Effective Date
+verify_result
is
array
Certification return Information
++liveness_score
is
string
In vivo test score
++score
is
string
Face real-name authentication
++spoofing
is
string
Score of composite map is returned 0 if composite map detection is not performed. Score of composite map detection is returned if in vivo detection is performed
+idcard_confirm
is
array
Id card information that the user confirms twice
++name
is
string
The name
++idcard_number
is
string
Id number
· Return results
{"idcard_ocr_result": {"address": "XXX", "birthday": "19691211", "name": "XX" sun, "id_card_number" : "320821 XXXXXXX", "gender" : "female", "nation" : "han", "expire_time" : "XXX," "issue_authority" : "XXX", "issue_time": "XXX"}, "verify_result": {"liveness_score": 0.39, "score": 11.65885925}, "idcard_confirm": {"name": "sun XX", "idcard_number": "320821XXXXXXX"}}, "log_id": "1436111694"}Copy the code