SDK Calling Example

There are two service authentication modes: This section uses Image Moderation as an example to describe how to use AK/SK-based and token-based authentication. The corresponding URI is POST /v1.0/moderation/image.

Using SDK in AK/SK-based Authentication Mode

  1. Set app_key and app_secret in the image_moderation_aksk_demo.js file. The sample code is as follows:
    1
    2
    3
    4
    // CN North-Beijing1 (cn-north-4) is supported.
    utils.initRegion("cn-north-4");
    var app_key = "*************";
    var app_secret = "************";
    
  2. Modify data_url or filepath of the image in the image_moderation_aksk_demo.js file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace ./data/moderation-terrorism.jpg in filepath with the path of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      6
      var filepath = "./data/moderation-terrorism.jpg";
      var data = utils.changeFileToBase64(filepath);
      
      content.image_content_aksk(app_key, app_secret, data, "", ["politics"], "", function (result) {
          console.log(result);
      });
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of demo_data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      6
      // The OBS link must be consistent with the region. The OBS resources cannot be shared among different regions.
      demo_data_url = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg";
      
      content.image_content_aksk(app_key, app_secret, "", demo_data_url, ["politics"], "", function (result) {
          console.log(result);
      });
      
  3. Execute the image_moderation_aksk_demo.js file. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    4
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    
    Process finished with exit code 0
    

Using SDK in Token-based Authentication Mode

  1. In the image_moderation_token_demo.js file, set the registered username and password. The username of a non-IAM login user is the same as the domain name of an IAM login user. The sample code is as follows:
    1
    2
    3
    4
    5
    // CN North-Beijing1 (cn-north-4) is supported.
    utils.initRegion("cn-north-4");
    var username = "*******"; // Configure the username.
    var domain_name = "*******"; // Configure the domain name.
    var password = "*******"; // Configure the password.
    
  2. Modify demo_data_url or filepath of the image in the image_moderation_token_demo.js file. Image Moderation supports two calling methods: file calling and URL calling.
    • To call the file, modify the local path of the target image. Specifically, replace ./data/moderation-terrorism.jpg in filepath with the path of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      6
      7
      8
      var filepath = "./data/moderation-terrorism.jpg";
      var data = utils.changeFileToBase64(filepath);
      
      token.getToken(username, domain_name, password, function (token) {
          content.image_content(token, data, "", ["politics"], "", function (result) {
              console.log(result);
          });
      });
      
    • To call the URL, modify the URL of the target image. Specifically, replace the image URL of demo_data_url with the URL of the target image. The sample code is as follows:
      1
      2
      3
      4
      5
      6
      7
      8
      // The OBS link must be consistent with the region. The OBS resources cannot be shared among different regions.
      demo_data_url = "https://ais-sample-data.obs.cn-north-1.myhuaweicloud.com/terrorism.jpg";
      
      token.getToken(username, domain_name, password, function (token) {
          content.image_content(token, "", demo_data_url, ["politics"], "", function (result) {
              console.log(result);
          })
      });
      
  3. Execute the image_moderation_token_demo.js file. If the moderation result is displayed on the console, the execution is successful.
    1
    2
    3
    4
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    {"result":{"detail":{"politics":[]},"suggestion":"pass","category_suggestions":{"politics":"pass"}}}
    
    Process finished with exit code 0