简介

欢迎使用腾讯云开发者工具套件(SDK)3.0,SDK3.0 是云 API3.0 平台的配套工具。目前已经支持 cvm、vpc、cbs 等产品,后续所有的云服务产品都会接入进来。新版 SDK 实现了统一化,具有各个语言版本的 SDK 使用方法相同,接口调用方式相同,统一的错误码和返回包格式这些优点。

腾讯云提供了多种语言版本的实现,有PHP、Python、Java 、Go、.NET、Node.js。

各语言版本:https://cloud.tencent.com/document/sdk

和加固有关的接口:https://cloud.tencent.com/document/api/283/17742

secretId和secretKey获取:https://console.cloud.tencent.com/cam/capi

API Explorer:https://console.cloud.tencent.com/api/explorer,提供了在线调用、签名验证、 SDK 代码生成和快速检索接口等能力,能显著降低使用云 API 的难度,推荐使用。

本文以Java语言为基础,其他语言请参考各SDK。推荐使用IntelliJ IDEA,新建maven工程,引入腾讯云公共jar包,并和自己的代码打包成一个完整jar包。

依赖版本号查询:https://mvnrepository.com/artifact/com.tencentcloudapi/tencentcloud-sdk-java

<dependency>
     <groupId>com.tencentcloudapi</groupId>
     <!-- 注:这里的版本号请参考github或者maven仓库最新版本号  -->
     <artifactId>tencentcloud-sdk-java</artifactId>
     <version>3.0.8</version>
</dependency>

1. 接口描述

主要介绍和加固直接相关的接口;

接口名称 接口功能 备注
CreateResourceInstances 创建资源 白名单用户才能使用
CreateShieldInstances 提交加固基础数据 创建加固请求
CreateShieldPlanInstance 新增加固策略 专业版以上用户使用
DeleteShieldInstances 批量删除提交过的app信息 删除加固历史记录
DescribeShieldInstances 用户查询提交过的app列表 查看加固历史记录
DescribeShieldPlanInstances 查询加固策略 专业版以上用户使用
DescribeShieldResult 查询加固结果 查询结果
DescribeResourceInstances 获取用户的所有资源信息 专业版以上用户使用

2. 接口调用逻辑

2.1 免费用户(基础版)

CreateShieldInstances 提交加固基础数据;

必传参数:待加固apk的下载链接,不支持直接上传apk文件;该apk的md5值,确保服务器下载的文件是正确的;
代码中字符串已经转义,可直接填写apk的下载链接和md5值,回调地址CallbackUrl为加固结果通知接口,可设置为空。如果为空字符串,就需要用户定时查询加固结果。基础版PlanId为0。
请求成功会返回ItemId字段,该字段可用于查询加固结果。

public class CreateShieldInstance{
     public static void main(String [] args) {
         try{
            Credential cred = new Credential("你的secretId", "你的secretKey");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            MsClient client = new MsClient(cred, "", clientProfile);
            String params = "{" +
                "\"AppInfo\":" +
                 "{\"AppUrl\":\"apk下载链接\",\"AppMd5\":\"apk Md5值\"}" +                 "," +
                "\"ServiceInfo\":" +
                "{\"ServiceEdition\":\"basic\",\"CallbackUrl\":\"回调地址,可为空字符串\",\"SubmitSource\":\"api\",\"PlanId\":0}" +
                "}";
            CreateShieldInstanceRequest req = CreateShieldInstanceRequest.fromJsonString(params, CreateShieldInstanceRequest.class);
            CreateShieldInstanceResponse resp = client.CreateShieldInstance(req);
            System.out.println(prettyJson(CreateShieldInstanceRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
                 System.out.println(e.toString());
         }
     }
     public static String  prettyJson(String json){
    	Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser parser = new JsonParser();
   	JsonElement element = parser.parse(json);
     	return gson.toJson(element);
    }
}

DescribeShieldResult 查询加固结果;必传参数为ItemId,该字段来自提交加固接口的返回值;

如果加固成功会返回加固包下载地址,该地址有有效期,请尽快下载;
可以写一个定时查询;

public class DescribeShieldResult{
      public static void main(String [] args) {
       try{
           Credential cred = new Credential("你的secretId", "你的secretKey");
           HttpProfile httpProfile = new HttpProfile();
           httpProfile.setEndpoint("ms.tencentcloudapi.com");
           ClientProfile clientProfile = new ClientProfile();
           clientProfile.setHttpProfile(httpProfile);            
           MsClient client = new MsClient(cred, "", clientProfile);
           String params = "{\"ItemId\":\"提交加固成功返回的ItemId\"}";
           DescribeShieldResultRequest req = DescribeShieldResultRequest.fromJsonString(params, DescribeShieldResultRequest.class);
           DescribeShieldResultResponse resp = client.DescribeShieldResult(req);
           System.out.println(prettyJson(DescribeShieldResultRequest.toJsonString(resp)));
       } catch (TencentCloudSDKException e) {
               System.out.println(e.toString());
       }
   }
}

2.2 付费用户(专业版、企业版)

线上购买服务:https://buy.cloud.tencent.com/ms#1

购买成功后,在控制台移动安全产品区域,可看到已经购买的服务:https://console.cloud.tencent.com/ms/service/list

此时购买的服务相当于兑换券资源,还需要和具体的APK绑定;这个网页上的绑定操作和绑定接口:https://cloud.tencent.com/document/api/283/18579 是一样的,可直接在网页上操作绑定成功;点击上图右侧立即绑定,如果绑定成功,会转变为详情,详情即为具体绑定的apk信息。一个资源对应一个apk,绑定成功即消耗掉该资源。

绑定成功后,可获取用户所有的资源信息;DescribeResourceInstances

如果接口调用成功,会返回ResourceId字段,需要保存该字段;

   public class DescribeResourceInstances
   {
   public static void main(String [] args) {
       try{
           Credential cred = new Credential("你的secretId", "你的secretKey");
           HttpProfile httpProfile = new HttpProfile();
           httpProfile.setEndpoint("ms.tencentcloudapi.com");
           ClientProfile clientProfile = new ClientProfile();
           clientProfile.setHttpProfile(httpProfile);            
           MsClient client = new MsClient(cred, "", clientProfile);
           String params = "{\"Pids\":[13624,12750]}";//13624加固专业版,12750加固企业版
           DescribeResourceInstancesRequest req =  DescribeResourceInstancesRequest.fromJsonString(params, DescribeResourceInstancesRequest.class);
           DescribeResourceInstancesResponse resp = client.DescribeResourceInstances(req);
           System.out.println(DescribeResourceInstancesRequest.toJsonString(resp));
       } catch (TencentCloudSDKException e) {
               System.out.println(e.toString());
       }
   }
}

获取PlanId;通过查询加固策略接口来获取PlanId字段;DescribeShieldPlanInstances

如果接口调用成功,请保存PlanId字段,同时也可看到该资源绑定的apk包名信息;

   public class DescribeShieldPlanInstance
   {
   public static void main(String [] args) {
       try{
           Credential cred = new Credential("你的secretId", "你的secretKey");
           HttpProfile httpProfile = new HttpProfile();
           httpProfile.setEndpoint("ms.tencentcloudapi.com");
           ClientProfile clientProfile = new ClientProfile();
           clientProfile.setHttpProfile(httpProfile);            
           MsClient client = new MsClient(cred, "", clientProfile);
           String params = "{\"ResourceId\":\"上面接口返回的ResourceId字段\"" +
               "," +
               "\"Pid\":13624" +
               "}";//如果是13624则为专业版,12750为企业版
           DescribeShieldPlanInstanceRequest req = DescribeShieldPlanInstanceRequest.fromJsonString(params, DescribeShieldPlanInstanceRequest.class);
           DescribeShieldPlanInstanceResponse resp = client.DescribeShieldPlanInstance(req);
           System.out.println(DescribeShieldPlanInstanceRequest.toJsonString(resp));
       } catch (TencentCloudSDKException e) {
               System.out.println(e.toString());
       }
   }
   }

提交加固基础数据 CreateShieldInstances

提交加固的apk包名信息,需要和资源绑定的apk包名信息一致

public class CreateShieldInstance{
     public static void main(String [] args) {
         try{
            Credential cred = new Credential("你的secretId", "你的secretKey");
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("ms.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            MsClient client = new MsClient(cred, "", clientProfile);
            String params = "{" +
                "\"AppInfo\":" +
                 "{\"AppUrl\":\"apk下载链接\",\"AppMd5\":\"apk Md5值\"}" +                 "," +
                "\"ServiceInfo\":" +
                "{\"ServiceEdition\":\"professional或enterprise\",\"CallbackUrl\":\"回调地址,可为空字符串\",\"SubmitSource\":\"api\",\"PlanId\":上一个接口返回的PlanId,整数}" +
                "}";
             CreateShieldInstanceRequest req = 		     CreateShieldInstanceRequest.fromJsonString(params, CreateShieldInstanceRequest.class);
             CreateShieldInstanceResponse resp = client.CreateShieldInstance(req);          System.out.println(prettyJson(CreateShieldInstanceRequest.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
                 System.out.println(e.toString());
         }
     }
     public static String  prettyJson(String json){
    	Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser parser = new JsonParser();
   	JsonElement element = parser.parse(jjavason);
     	return gson.toJson(element);
    }
}

查询加固结果 DescribeShieldResult

和上面免费版是一样的方式,拿到提交加固接口返回的ItemId,定时调用查询加固结果的接口。

成功后获取到下载链接,及时下载;下载完成后,重签名并测试是否正常运行。

文章来源于腾讯云开发者社区,点击查看原文