In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the use of JAVA to develop Wechat public platform environment building and development access example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!
I. the initial Wechat public platform
Wechat public platform, which is what we usually call "official account", was once named "official platform" and "media platform", but it was eventually named "public platform". From the naming of Wechat, I can find that the public platform is not only a platform used by the official and media, but also a unified platform open to all members of the public.
Wechat public platform address: https://mp.weixin.qq.com/
Wechat public platform is divided into 4 sections: Subscription account, service number, Mini Program and enterprise account. According to the Wechat open course plan in 2016, Enterprise will merge with WeCom in the future, so we will focus on the first three parts:
Four sections of Wechat platform
Briefly compare the differences between the first three and the focus of this course, that is, the follow-up course:
1. Subscription account and the service number are both traditional "official accounts" with the ability to send messages in large groups. Detailed differences can be found on the official platform: http://kf.qq.com/faq/140806zARbmm140826M36RJF.html we will focus on the development of service capabilities, but in terms of services, the two development models are exactly the same, except that the service number can use more service interfaces, while Subscription account is the "castrated version" service number. Therefore, we will use the service number for the case in the follow-up course.
2. WeChat Mini Programs was originally a "Wechat app", that is, an APP rooted in the Wechat ecosystem. Due to the limitations of Apple and Google, the "app number" was aborted before it was launched, and it was replaced by the moderately castrated "WeChat Mini Programs", and its operating experience comparable to that of the native APP made Mini Program the development direction of the current popularity. And Jerry Education's brand-new H5 development curriculum will also join the popular WeChat Mini Programs development.
2. Development account preparation 1. Account registration
Enter the Wechat public platform https://mp.weixin.qq.com, click the upper right corner to register immediately, and select "Service number" or "Subscription account" to register (the service number is for institutional users only, individual users can only choose Subscription account)
You need to fill in a series of information when registering, and you can fill in according to the prompts. I will not repeat them here.
2. Test account application
If you do not want to register an account for the time being, or if you are unable to register a service number, you can choose the official test account application. Application address: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login click and scan the QR code, you can get a test account, with full permissions of the service number.
Test the account interface
Preparation of development environment 1. External network mapping tool
When visiting the backend, Wechat public platform must provide a public network address that can be accessed correctly. There are two requirements for backend URL for Wechat platform:
① must be able to access ② through public network. Port 80 must be used.
To achieve the above two points, we can choose to purchase public network servers, such as Aliyun, Baidu Cloud and Tencent Cloud. They're all good choices. If there is no server, you can choose to use the external network mapping tool to map our intranet links to the public network, the better mapping software are: peanut shell, ngrok, nat123 and so on.
These softwares can be easily searched and downloaded by Baidu. Here is a brief explanation of how to use them:
① Ngrock:
Enter the dos environment, switch to the drive letter where ngrock is located, and enter ngrock8080:
Ngrock operation
Enter and wait for a while to get the public network link. The link shown in the shaded area shown in the following figure can directly access the link content under the native 127.0.0.1 8080, which is the address corresponding to the http protocol and the https protocol:
Interface for successful ngrock operation
② peanut shell and nat123 can be operated in the software after installing the software. For more information, please see Baidu experience: http://jingyan.baidu.com/article/363872ec361d3f6e4ba16ff9.html will not repeat it here.
IV. The principle of data interaction on Wechat public platform
After the public network access address is set up, the background code developed by us can be placed on the public network address, so how do users access our code?
Let's take a look at the principle of data interaction on the Wechat public platform:
As can be seen from the image above, the Wechat public platform actually acts as a bridge, and the code that actually handles business and provides services is still placed on our own server or public network mapping.
In that case, we can write background code on our own computer (server), provide a URL accessible to the public network through a mapping tool, and then bind this URL to the Wechat backend.
V. access to the development mode
The following will enter the actual development process, we can refer to the official development documentation: https://mp.weixin.qq.com/wiki
1. Fill in the server configuration
Enter Wechat public platform, click * * Development-basic configuration * * on the left, and select * * Server configuration * *. You can go to the configuration page:
Server configuration for Wechat platform
Where:
URL: the public network access address of the backend server we mentioned above.
Token: the authentication password defined by the developer
EncodingAESKey: random string, which needs to be verified only if the message is encrypted and decrypted in secure mode.
2. Verify whether the message is valid
When we click submit, the Wechat server will send a Get request to our above address, passing in four parameters:
Get request parameters of Wechat server
We verify the request by verifying the signature (there is a verification method below). If you confirm that the GET request is from Wechat server and return the echostr parameter content as is, the connection will take effect and become a successful developer, otherwise the connection will fail. The encryption / verification process is as follows:
Next, we implement the code operation.
6. Develop access background code
1. Use MyEclipse to create a Web project and create a new servlet:
Create a new servlet
2. Obtain the above four verification parameters in the doGet method of servlet:
Get the above four check parameters
3. Write a tool class to verify the operation of the method:
Write tool classes
For verification steps, refer to the above [5-2] three-step operation flow.
Check step
Use the sha1 encryption method ↓
Sha1 encryption method
4. The verification method is called in Servlet, and the result is verified. If the verification is successful, the random string eahostr is returned to the Wechat platform:
Call the check method in Servlet
At this point, the Servlet and Check tool classes have been written.
5. Start Tomcat, and map the local address of Servlet (for example, local localhost:8080/WeiXin/servlet/WeiXinServlet) to the public network. Refer to the third part above, and map the public network to make sure that the public network address can be accessed correctly.
I will directly put the code on the public network server for access. Get the following address
7. Configure the backstage of the public platform
Enter the configuration information of Wechat backend:
Information about backend configuration of Wechat
Click submit, Wechat will send a Get instruction to Servlet, and call the doGet method to carry out the verification operation we wrote. Finally, if the random string is returned successfully, the binding will be successful.
Source code sharing 1. Servlet source code (only doGet part is retained): public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String signature = request.getParameter ("signature"); String timestamp = request.getParameter ("timestamp"); String nonce = request.getParameter ("nonce"); String echostr = request.getParameter ("echostr"); PrintWriter out = response.getWriter () If (CheckUtil.checkSignature (signature, timestamp, nonce) {/ / if the verification is successful, the resulting random string will be returned to out.print (echostr);}} 2, CheckUtil source code (you can directly Copy the getSha1 encryption method in it): package com.jredu.util;import java.security.MessageDigest;import java.util.Arrays;public class CheckUtil {public static final String tooken = "jredu100"; / / developers define Tookenpublic static boolean checkSignature (String signature,String timestamp,String nonce) {/ / 1. Define the array to store tooken,timestamp,nonceString [] arr = {tooken,timestamp,nonce}; / / 2. Sort the array Arrays.sort (arr); / / 3. Generate the string StringBuffer sb = new StringBuffer (); for (String s: arr) {sb.append (s);} / / 4.sha1 encryption, there are ready-made codes on the Internet String temp = getSha1 (sb.toString ()); / / 5. Compare the encrypted string with the encrypted signature sent from Wechat, and return the result return temp.equals (signature);} public static String getSha1 (String str) {if (str==null | | str.length () = = 0) {return null;} char hexDigits [] = {'0,000,1, 3, 4, 5, 6, 7, 8, 9, 9, 9, 4, 6, 6, 6, 9, 9, 6, 6, 6, 6, 9, 9, 6, 6, 6, 6, 9, 6, 6, 6, 6, 9, 6, 6, 6, 6, 6, 6, 9, 6, 6, 6, 6, 6, 9, 6, 6, 6, 6, 9, 9, 6, 6, 9, 6, 6, 6, 9, 6, 6, 6, 9, 9, 6, 6, 6, 6, 9, 6, 6, 6, 9, 9, 6, 6, 6, 6, 6, 6, 9, 6, 6, 6, 6, 9, 9, 6, Try {MessageDigest mdTemp = MessageDigest.getInstance ("SHA1"); mdTemp.update (str.getBytes ("UTF-8")); byte [] md = mdTemp.digest (); int j = md.length;char buf [] = new char [juni2]; int k = 0 for (int I = 0; I)
< j; i++) {byte byte0 = md[i];buf[k++] = hexDigits[byte0 >> 4 & 0xf]; buf [knot +] = hexDigits [byte0 & 0xf];} char hexDigits [] = {'0,000,6, 4, 5, 6, 8, 9, 9, 6, 8, 9, 9, 9, 9, 6, 6, 6, 6, 8, 9, 9, 9, 9, 9, 9, 6, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 6, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 6, 6, 6, 6, 6, 6, 8, 8, 9, 9, 9, 9, 6, 6, 6, 6, 6, 8, 8, 9, 9, 9, 6, 6, 6, 6, 6, 8, 9, 9, 9, 9, 6, 6, 6, 6, 6, 6, 8, 8, 9, 9, 9, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6 Int j = md.length;char buf [] = new char [juni2]; int k = 0 for (int I = 0; I)
< j; i++) {byte byte0 = md[i];buf[k++] = hexDigits[byte0 >> 4 & 0xf]; buf [KBG +] = hexDigits [byte0 & 0xf];} return new String (buf);} catch (Exception e) {/ / TODO: handle exceptionreturn null;} these are all the contents of this article entitled "sample Analysis of Environment Construction and Development access for developing Wechat Public platform using JAVA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.