Step By Step
1、API请求参数解释说明
2、Nuget安装SDK:aliyun-net-sdk-onsmqtt
3、Code Sample
using System;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.OnsMqtt.Model.V20200420;
namespace MQTTForIoT
{
class Program
{
static void Main(string[] args)
{
// 公网区域实例测试 https://developer.aliyun.com/article/693979?spm=a2c6h.13262185.0.0.15ba5eb5T92u4V
IClientProfile profile = DefaultProfile.GetProfile("mq-internet-access", "LTAIOZZg********", "v7CjUJCMk7j9aK****************");
DefaultAcsClient client = new DefaultAcsClient(profile);
var request = new ApplyTokenRequest();
request.Resources = "<parent_topic>/#";
request.InstanceId = "post-cn-6ja********";
// 当前时间 + 30分钟
long expireTime = ((DateTime.Now.AddMinutes(30).Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)).Ticks) / 10000);
request.ExpireTime = expireTime;
request.Actions = "R,W";
Console.WriteLine("expireTime: " + expireTime);
try
{
var response = client.GetAcsResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
Console.ReadKey();
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
}
}
}
4、The Result
expireTime: 1603198649047
{"RequestId":"D03E4AEC-3628-4050-A9EA-4CFF111111AF","Token":"1111+XLFl5s/YWJ/MlDz4t/Lq5HC11111128HAMaxYzmBSHQsWXgdISJ1ZJ+2cxaqqEoGlfCn2imhZ4FoNx3kipRDnjsfBujbJGYgJWUr5piesdvDY0i8fNY68mR3UqN9LKVT5IGzKPvooOIjF1CZZ9uU74CT40m4bkmcftVUBP5SM+VepMKCyQgoJWL8b3AQUS1QPxDA2oGf+JBKuN0DyYW6d7mIYhAqXTpVbQw5nNCvKP80Xo0WYK9UHMgTMh9qdrn6MS1rwaP765dpXzvgHC9nWeHX7K80O6vtOU9M8Qn5VrhkP0F1umbOoYs3NfM+WYZIQx4pkViQo6qqkxgbD7le+3be3pC6mQHEsLVxniQFOoUUHHfSw=="}