C++ SDK
Updated at:
This topic provides an example of how to use the C++ SDK.
Get the SDK
git clone https://github.com/aliyun/aliyun-openapi-cpp-sdk.gitNote:An AccessKey for an Alibaba Cloud account has permissions to access all APIs, which poses a high security threat. For API access or daily operations and maintenance (O&M), create and use a Resource Access Management (RAM) user. To create a RAM user, log on to the RAM console.
To prevent key leaks, do not hardcode your AccessKey ID and AccessKey secret in your code. Instead, use environment variables to store and access them.
On Linux and macOSexport NLP_AK_ENV=<access_key_id>
export NLP_SK_ENV=<access_key_secret>Replace <access_key_id> with your AccessKey ID and <access_key_secret> with your AccessKey secret.
On Windows, add the NLP_AK_ENV and NLP_SK_ENV environment variables. Set their values to your AccessKey ID and AccessKey secret. Then, restart Windows.
Usage example
#include <iostream>
#include <alibabacloud/core/AlibabaCloud.h>
#include <alibabacloud/nlp-automl/Nlp_automlClient.h>
#include <cstdlib> // Include the header file for the getenv function.
using namespace AlibabaCloud;
using namespace AlibabaCloud::Nlp_automl;
int main(int argc, char** argv) {
// Initialize the SDK.
AlibabaCloud::InitializeSdk();
const char* access_key_id = std::getenv("NLP_AK_ENV");
const char* access_key_secret = std::getenv("NLP_SK_ENV");
// Configure the nlp_automl instance.
ClientConfiguration configuration("<your-region-id>"); // For example, "cn-hangzhou".
Nlp_automlClient client(access_key_id, access_key_secret, configuration);
// Create an API request and set the parameters.
Model::GetPredictResultRequest request;
request.setModelId(1818);
request.setContent("People's Liberation Army of the People's Republic of China");
auto outcome = client.getPredictResult(request);
if (!outcome.isSuccess()) {
// Handle exceptions.
std::cout << outcome.error().errorCode() << std::endl;
AlibabaCloud::ShutdownSdk();
return -1;
}
std::cout << "totalCount: " << outcome.result().getTotalCount() << std::endl;
// Shutdown the SDK.
AlibabaCloud::ShutdownSdk();
return 0;
}Is this page helpful?