Help Center> Content Moderation> SDK Reference> Using the SDK (Java)> SDK Projects Using Token Authentication

SDK Projects Using Token Authentication

Two authentication methods, token authentication and AK/SK authentication, are available. This section uses token authentication as an example.

(You can understand porn detection after performing the following operations. You only need to modify the username and password instead of compiling code.)

Sample Code

  1. Set projectName in TokenDemo.java.
    1
    2
    3
    4
    5
    6
    7
    /**
     * Access the service using token authentication.
     */
    public class TokenDemo {
    private static final String projectName = "cn-north-1";     // Configuration of Content Moderation in the CN North-Beijing1 (cn-north-1) region.
    	private static final String URL_TEMPLATE = ServiceAccessBuilder.getCurrentEndpoint(projectName)+"/v1.0/moderation/image/batch?job_id=%s";
    	private static final long POLLING_INTERVAL = 2000L;
    
  2. Open the TokenDemo.java file in the com.huawei.ais.demo package, and change the values of username and password in the main function to the actual username and password registered with the system. The sample code is as follows:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    /**
     * Invoke the main entrypoint function.
     */
    public static void main(String[] args) throws URISyntaxException, UnsupportedOperationException, IOException {
        String username = "zhangshan";      // Enter the username.
        String password = "*******";        // Enter the password.
        String domainName = "*******";      
        String token = getToken(username, password, domainName, projectName);
        System.out.println(token);
    		
        // Configure the following three timeout limits:
        connectionTimeout = 5000; //Timeout limit for connecting to the target URL
        connectionRequestTimeout = 1000;//Timeout limit for obtaining available connections from the connection pool
        socketTimeout = 5000;//Timeout limit for obtaining a server response
    				
    		
         //Run Image Clarity Detect.
        //requestModerationClarityBase64(token, "data/moderation-demo-1.jpg");
    		
        //Run Distortion Correction.
        //requestModerationDistortionCorrectBase64(token, "data/moderation-demo-1.jpg");
    		
        //Run Text Moderation.
        //requestModerationTextContentBase64(token, "6666666666");
    		
        //Run Image Moderation.
        //requestModerationImageContentBase64(token, "data/moderation-demo-1.jpg");
    
         //Run Image Moderation (asynchronous batch task).
        String url1 = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg";
        String url2 = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/antiporn.jpg";
        requestModerationImageContentBatchJobs(token, new String[]{url1,url2});
    
            //Run Image Moderation (batch task).
        //requestModerationImageContentBatch(token, new String[]{url1,url2});
    }
    
  3. After modification, run the TokenDemo.java file. On the console, you can view the recognition result using token authentication.