Updated on 2025-07-02 GMT+08:00

Text Moderation (V3)

  1. This section describes how to use the Text Moderation SDK by AK/SK authentication.

    Change the value of Text in TextDetectionDataReq in the sample code to the text to be detected. Then run the code.

     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
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    using System;
    using System.Collections.Generic;
    using HuaweiCloud.SDK.Core;
    using HuaweiCloud.SDK.Core.Auth;
    using HuaweiCloud.SDK.Moderation;
    using HuaweiCloud.SDK.Moderation.V3;
    using HuaweiCloud.SDK.Moderation.V3.Model;
     
    namespace RunTextModerationSolution
    {
        class Program
        {
            static void Main(string[] args)
            {
           //Hard-coded or plaintext AK and SK are risky. For security purposes, encrypt your AK and SK and store them in the configuration file or environment variables.
          //In this example, the AK and SK are stored in environment variables for identity authentication. Before running this example, configure environment variables HUAWEICLOUD_SDK_AK and HUAWEICLOUD_SDK_SK.
            string ak= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_AK", EnvironmentVariableTarget.Machine);
            string sk= Environment.GetEnvironmentVariable("HUAWEICLOUD_SDK_SK", EnvironmentVariableTarget.Machine);
     
                var config = HttpConfig.GetDefaultConfig();
                config.IgnoreSslVerification = true;
                var auth = new BasicCredentials(ak, sk);
     
                var client = ModerationClient.NewBuilder()
                .WithCredential(auth)
                .WithRegion(ModerationRegion.ValueOf("xxx"))  //Replace xxx with the service region, for example, ap-southeast-3.
                .WithHttpConfig(config)
                .Build();
     
                var req = new RunTextModerationRequest
                {
                };
                TextDetectionDataReq databody = new TextDetectionDataReq()
                {
                    Text = "test"
                };
                req.Body = new TextDetectionReq()
                {
                    Data = databody,
                    EventType = "comment"
                };
     
                try
                {
                    var resp = client.RunTextModeration(req);
                    var respStatusCode = resp.HttpStatusCode;
                    Console.WriteLine(respStatusCode);
                }
                catch (RequestTimeoutException requestTimeoutException)
                {
                    Console.WriteLine(requestTimeoutException.ErrorMessage);
                }
                catch (ServiceResponseException clientRequestException)
                {
                    Console.WriteLine(clientRequestException.HttpStatusCode);
                    Console.WriteLine(clientRequestException.ErrorCode);
                    Console.WriteLine(clientRequestException.ErrorMsg);
                }
                catch (ConnectionException connectionException)
                {
                    Console.WriteLine(connectionException.ErrorMessage);
                }
            }
        }
    }
    
  2. If 200 is displayed on the console, the code is successfully executed. The text moderation result is displayed on the console.
    200
     {
         "request_id":"d81702cfd3599e103030c3d52d6f955f",
         "result":{
             "suggestion":"pass",
             "label":"normal",
             "details":[]
         }
     }