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
- 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 = "************";
- 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); });
- 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:
- 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
- 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.
- 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); }) });
- 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:
- 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
Last Article: Importing a Node.js SDK into a Node.js Environment
Next Article: Change History
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.