阿里云微服务消息队列Token Java Code Sample-阿里云开发者社区

开发者社区> 云服务技术课堂> 正文

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

简介: 文本主要演示使用Java SDK调用 ApplyToken 创建临时访问 Token。

Step By Step

1、API请求参数解释说明图片.png

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

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:

云服务技术课堂,各类技术课程、最佳实践输出,来好好听课吧!

官方博客