C#如何调用阿里云百炼

更新时间: 2023-11-22 18:28:13

本文介绍如何使用C#语言调用阿里云百炼。

操作步骤

1. 安装C# SDK

 dotnet add package AlibabaCloud.SDK.Bailian20230601

2. 调用阿里云百炼文本生成接口

a. 通过C#SDK生成临时Token

using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.Bailian20230601;
using AlibabaCloud.SDK.Bailian20230601.Models;


Config config = new Config();
config.AccessKeyId = "********";  //**为脱敏数据
config.AccessKeySecret = "********";
config.Endpoint = "bailian.cn-beijing.aliyuncs.com";

Client client = new Client(config);
CreateTokenRequest request = new CreateTokenRequest();
request.AgentKey = "********";
CreateTokenResponse response = client.CreateToken(request);

CreateTokenResponseBody body = response.Body;
if (body == null)
{
    throw new ApplicationException("create token error");
}

if (body.Success != true)
{
    String error = body.Message +", requestId: " + body.RequestId;
    throw new ApplicationException(error);
}

CreateTokenResponseBody.CreateTokenResponseBodyData data = body.Data;
if (data == null) {
    throw new ApplicationException("create token error, data is null");
}

Console.WriteLine("token: {0}, expired time: {1}", data.Token, data.ExpiredTime);
说明

在调用文本生成接口时,建议保存token和有效期时间,在过期前10分钟, 再次调用生成token代码。

b. 参考Competion API文档调用文本生成接口。

阿里云首页 相关技术圈