阿里云微服务消息队列Token Java Code Sample

Step By Step

1、API请求参数解释说明阿里云微服务消息队列Token Java Code Sample

2、pom.xml

<dependencies>
        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-onsmqtt -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-onsmqtt</artifactId>
            <version>1.0.4</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.5.6</version>
        </dependency>
    </dependencies>

3、Code Sample

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.onsmqtt.model.v20200420.ApplyTokenRequest;
import com.aliyuncs.onsmqtt.model.v20200420.ApplyTokenResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

public class ApplyTokenDemo {

    public static void main(String[] args) {
        // https://developer.aliyun.com/article/693979?spm=a2c6h.13262185.0.0.1a7f5eb5mZCprP
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAIOZZg********", "v7CjUJCMk7j9aK****************");
        IAcsClient client = new DefaultAcsClient(profile);

        // https://help.aliyun.com/document_detail/163049.html?spm=a2c4g.11186623.6.608.12d642cal4i2Yn
        ApplyTokenRequest request = new ApplyTokenRequest();
        request.setResources("topicdemo/#");
        request.setInstanceId("post-cn-0pp13******");
        request.setExpireTime(System.currentTimeMillis() + 1000*60*10); // 过期时间为当前时间 + 10分钟
        request.setActions("R,W");

        try {
            ApplyTokenResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

4、The Result

{"requestId":"5E501111-AA2E-4AA8-9330-369CF14B1111","token":"LzMT+XLFl5s/YWJ1111z4t/Lq5HC1iGU1P28HAMaxYzmBSHQsWXgdISJ1ZJ+2cxa6OGEoSNYNUwKHx3Gb0+RQ/MkFF0ukQBAbJGYgJWUr5piesdvDY0i8Q0UdBDB9AhlRN7WXe/fMmbvooOIjF1CZZ91111CT40m4bkmcftVUBP5SM+VepMKCyQgoJWL8b3AwS/EK73BBYd/bO6tnAwvMaNL3uAnaqTRIfIBM10mgCpKQvYrkhktWtnCUFsi2QDWYIMnRJvEpgay7eFCMO4HabQ8y8ysZbQ1UI4BY0leKhBc2NXexKqBNE1VjqqJk/klr2oiya12mL8tTlXvs9rXpzOQGdXskhA6"}

更多参考

ApplyToken

上一篇:如何选择阿里云服务器ECS操作系统(新手说明指南)


下一篇:每周一企 | 「近屿智能」与钉钉联手赋能中小企业云上招聘