【Tencent Cloud Studio Practical Training Camp】Based on Cloud Studio through Java to achieve quick docking with the official account

Table of contents

1. What is Cloud Studio

1.1 Introduction to Cloud Studio

1.2 Features of Cloud Studio

1.3 Benefits of Cloud Studio

2. Practical cases

2.1 Create a development environment

2.2 Select a development template

2.3 Code writing

2.3.1 Import dependent packages

2.3.2 Create Models configuration class

2.3.3 Create test class demo.java

3. Use summary


Today, through the connection between Java and the WeChat official account, I will introduce Tencent Cloud Studio cloud development tools to you, and feel the efficiency improvement brought by its powerful cloud programming mode.

1. What is Cloud Studio

 

1.1 Introduction to Cloud Studio

Cloud Studio (Cloud IDE) is a browser-based integrated development environment that provides programmers with a safe, stable and fast cloud workstation. When using Cloud Studio, users don't need to install any software, just open a browser and log in to their account to write code anytime and anywhere. It also has efficient code collaboration. After the code is written and saved, the multi-person collaboration is updated almost in real time. And the team collaboration function also integrates very useful instant messaging functions, such as message boards, voice/video chat. This makes collaboration and communication more efficient and timely.

The predecessor of Cloud Studio is Coding WebIDE independently developed by CODING , so the login interface of Cloud Studio still retains the access entry prompt of the old version of WebIDE, which is convenient for old users to continue to access.

Official website introduction: Introduction to Cloud Studio (Cloud IDE) | Cloud Studio

1.2 Features of Cloud Studio

Online development environment: Cloud Studio can run directly in the browser without installing any local development tools. This allows developers to access and develop their projects anytime, anywhere. Of course, the official client tools are also very friendly to friends who love client development.

Support dozens of programming languages: Cloud Studio supports dozens of programming languages, including JavaScript, Python, Java, Go, C++, vue, nodejs and other templates. Developers can quickly develop projects in different programming languages ​​in one IDE.

Powerful code editor: Cloud Studio's editor provides common development functions such as syntax highlighting, code completion, automatic indentation, code folding, and multi-cursor editing, which greatly improves development efficiency.

Version control integration: Cloud Studio integrates common version control systems, such as Git, to facilitate developers to manage and submit code.

Cloud computing resources: Cloud Studio integrates Tencent Cloud's computing resources, including virtual machines, container services, and function computing. Developers can create and manage these resources directly in the IDE.

Code collaboration function: Developers can invite others to collaborate on the same project, share code editing and debugging in real time, and greatly improve team collaboration efficiency.

Security guarantee: Cloud Studio provides a secure development environment to protect developers' code and data security. At the same time, Tencent Cloud also provides a wealth of security control and authority management functions.

1.3 Benefits of Cloud Studio

Reduce the cost for developers to install development tools

Cloud development is very friendly to remote office, and it is not restricted by the office location, and can be developed at any time

Unified development environment, whether it is java, C#, vue, python, etc., a set of environments can develop projects in the corresponding programming language.

The teamwork ability is very strong, and it supports multi-person collaboration. After the code is updated, other team developers can see the code effect almost in real time

Reduce the cost of enterprise programmers' computers and save hardware resources

High security, Cloud Studio provides a secure development environment, protects developers' code and data security, and provides rich security control and authority management functions.

2. Practical cases

Let's introduce how to use Cloud Studio through a Java case.

2.1 Create a development environment

First, enter the official website and log in to your account, as shown in the figure below:

Here, select WeChat scan code to log in, and the interface after login is as follows:

2.2 Select a development template

Here you can see that the official built-in dozens of mainstream templates, because I am going to develop based on the Java language, click Java here.

Click to enter the project creation phase, the following figure is the creation process.

The creation process is relatively fast and can be created in less than a minute. After the creation is complete, the following picture is shown:

2.3 Code writing

After the development environment is created, we start writing the code.

2.3.1 Import dependent packages

First introduce dependencies, we open pom.xml, and increase the dependencies of the hutool tool library.

Add the following:

<!-- 引入hutool工具类库 -->
     <dependency>
          <groupId>cn.hutool</groupId>
          <artifactId>hutool-all</artifactId>
          <version>5.8.16</version>
      </dependency>

Then the development environment will automatically parse and download dependency packages. After the dependency package is installed, it can be used normally and there is no need to worry about it.

Brief introduction of Hutool

Hutool is a Java tool library that provides many commonly used functions and tool classes to simplify common tasks in the Java development process. Its goal is to provide a concise, efficient, and easy-to-use API to help developers write Java code more quickly and conveniently. The json and network request libraries are mainly used here.

2.3.2 Create Models configuration class

Create a Models directory to store configuration information, and then create a new ConstantUtil.java to store configuration information.

Note: In order to quickly demonstrate the effect, the configuration file is not used for the time being to manage the configuration information of the official account platform.

It is mainly the account information of the official account and the URL for calling the API. The specific configuration content is as follows:

package net.models;

/**
 * 配置类
 */
public class ConstantUtil {
        // 公众号开发者APPID
    public final static String app_id = "你的appId";
    // 公众号开发者密码
    public final static String app_secret = "你的appSecret";
    // Token获取
public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential";
    // 获取素材url
    public final static String news_url="https://api.weixin.qq.com/cgi-bin/material/batchget_material";
   
    
}

2.3.3 Create test class demo.java

First create the test folder, then create the Demo.java test class in the test folder

Explanation: To get the WeChat Token method, you only need to call the official get method to get the token information. This is the first step in the official account development and calling other interfaces.

   /**
     * 获取微信Token 使用 HttpUtil 请求类库获取token
     * 
     * @return
     */
    private static String GetWechatToken() {

        String tokenUrl = ConstantUtil.token_url + "&appid=" + ConstantUtil.app_id + "&secret="
                + ConstantUtil.app_secret;
        String tokenResult = HttpUtil.get(tokenUrl);
        System.out.println("返回的微信token信息");
        System.out.println(tokenResult);
        JSONObject tokenJson = JSONUtil.parseObj(tokenResult);
        String token = tokenJson.get("access_token").toString();
        return token;

    }

Note: We generally fail to obtain the token when we visit for the first time, because the WeChat public platform interface invokes a security mechanism, and we need to log in to our WeChat public account platform to set the ip whitelist.

Configuration method: Click to view, then modify the ip information, and finally use the administrator's WeChat to scan the code to confirm.

The error message of the first call is as follows

"{"errcode":40164,"errmsg":"invalid ip 101.34.119.114 ipv6 ::ffff:101.34.119.114, not in whitelist rid: 64cb0c69-57641460-58a91c42"}"

After the ip whitelist is configured, the WeChat Token can be obtained normally. As shown below:

How to obtain the list of public account picture materials

The code content is as follows:

    /**
     * 获取公众号图片素材列表
     */
    private static String GetArticleNew(String token) {
        // 定义body参数
        HashMap<String, Object> paramMap = new HashMap<>();

        String newsUrl = ConstantUtil.news_url + "?access_token=" + token;

        paramMap.put("type", "image");
        paramMap.put("offset", 100);
        paramMap.put("count", 2);

        String newResult = HttpUtil.post(newsUrl, JSONUtil.toJsonStr(paramMap));
        System.out.println("-----------------获取图文素材信息如下:-------------------------");
        System.out.println(newResult);       
        return newResult;

    }

Parameter description :

type: the type of material, picture (image), video (video), voice (voice), graphic (news) (required parameters)

offset: Return from the offset position of all materials, 0 means return from the first material (required parameter)

coun: Return the number of materials, the value is between 1 and 20 (required parameter)

The return format is as follows:

{
    "item":[
        {
            "media_id":"uk2hzL6i8MPTiBzig4LD64Kqv7UWho7VqENYuh2LLHSpqvP4_8L5Pf9MNTPhaaUi",
            "name":"9.jpg",
            "update_time":1645232103,
            "url":"https:\/\/mmbiz.qpic.cn\/sz_mmbiz_jpg\/HsDqsyKaPibH6vOqe6vcGIibZXwZiaZIxaicZiavib5xnzkDgTJ9YVUGX1NdgEPVnZ182Iic5p0txN05kqx6Np6bfVsyA\/0?wx_fmt=jpeg",
            "tags":[

            ]
        },
        {
            "media_id":"uk2hzL6i8MPTiBzig4LD66-YZlPjqi21gcgLlMzz0_55oKezz_5vLsmsI_OOev83",
            "name":"8.jpg",
            "update_time":1645232102,
            "url":"https:\/\/mmbiz.qpic.cn\/sz_mmbiz_jpg\/HsDqsyKaPibH6vOqe6vcGIibZXwZiaZIxaicgcbRYZgSI7smmtAR3d0kEicty5hmHCBkE6p6eHteIGFibNxdoEhxyrTQ\/0?wx_fmt=jpeg",
            "tags":[

            ]
        }
    ],
    "total_count":4559,
    "item_count":2
}

3. Use summary

Personally, I feel that the online cloud development environment of Cloud Studio is very good. Through this actual experience case of Java docking with the public account, the overall function is very smooth to use, project development anytime and anywhere, rich project templates, etc. With Cloud Studio this You are not affected by the office location of this tool, you can develop your own projects anytime, anywhere, and there are many advantages that you can experience for yourself.

 

Guess you like

Origin blog.csdn.net/xishining/article/details/132081457