开发者社区> 问答> 正文

如何创建授权

OnsEmpowerCreate 接口可以将目标 Topic 授权给指定用户使用,包括发布消息和订阅消息的权限。

使用场景

MQ 的 Topic 资源属于申请的 Owner 账号所有,当需要跨账号访问 Topic 资源时,Owner 账号可以使用授权接口对目标账号进行发布消息或者接收消息的权限授予。

授权注意事项:

授权可以支持三种权限:发布、订阅、发布&订阅。
发起授权的账号必须是资源的 Owner 账号,目标账号可以是子账号或者主账号。
授权后,目标账号仍然需要创建自己的订阅 CID,而不是使用 Owner 账号的 CID 来收消息。
请求参数列表

名称    类型    是否必须    描述
OnsRegionId    String    是    当前查询 MQ 所在区域,可以通过 OnsRegionList 方法获取
OnsPlatform    String    否    该请求来源,默认是从 POP 平台
PreventCache    Long    是    用于 CSRF 校验,设置为系统当前时间即可
EmpowerUser    Long    是    授权的目标用户 User ID,支持子账号 ID
Topic    String    是    授权的目标 Topic,该 Topic 必须是当前用户所有
Relation    Integer    是    授权类型,2代表授权发送,4代表授权订阅,6代表授权发送和订阅
返回参数列表

名称    类型    描述
RequestId    String    为公共参数,每个请求独一无二
HelpUrl    String    帮助链接
相关 API

OnsEmpowerList:查看授权列表
OnsEmpowerDelet:删除授权
使用示例

    public static void main(String []args) {
            public static void main(String []args) {
                    String regionId = "cn-hangzhou";
                    String accessKey = "XXXXXXXXXXXXXXXXX";
                    String secretKey = "XXXXXXXXXXXXXXXXX";
                    String endPointName ="cn-hangzhou";
                    String productName ="Ons";
                    String domain ="ons.cn-hangzhou.aliyuncs.com";
                    /**
                    *根据自己需要访问的区域选择Region,并设置对应的接入点
                    */
                    try {
                        DefaultProfile.addEndpoint(endPointName,regionId,productName,domain);
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                    IClientProfile profile= DefaultProfile.getProfile(regionId,accessKey,secretKey);
                    IAcsClient iAcsClient= new DefaultAcsClient(profile);
                    OnsEmpowerCreateRequest request = new OnsEmpowerCreateRequest();
                    request.setOnsRegionId("cn-qingdao-publictest");
                    request.setPreventCache(System.currentTimeMillis());
                    request.setAcceptFormat(FormatType.JSON);
                    request.setEmpowerUser("167535647680831312");
                    request.setTopic("topic_for_public_msg_test");
                    request.setRelation(2);
                    try {
                        OnsEmpowerCreateResponse response=iAcsClient.getAcsResponse(request);
                        System.out.println(response.getRequestId());
                    } catch (ServerException e) {
                        e.printStackTrace();
                    } catch (ClientException e) {
                        e.printStackTrace();
                    }
                }

展开
收起
猫饭先生 2017-10-27 13:44:33 1856 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载