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 > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Background
To develop Mini Program, you want to use the unique code of Wechat account. That's what API said.
Wx.login...
Code in exchange for session_key
Interface address:
Https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
problem
Open api and get, openid can be obtained by passing parameters to the correct browser address.
But
Mini Program requires that the requested domain name must be configured legally, while Wechat's api interface domain name is not allowed to be configured.
Solution.
Only backend services can write requests. Mini Program accesses its own server, and the server returns openid.
Concrete steps
Using Apache HttpClient to request third-party api interface
Provide several general classes that can be copied directly and then called
/ / if there is a code, it means WeChat Mini Programs. Obtain the authorization code String openid = "{ErrorMsg:\" acquisition failed according to code to identify which Mini Program to use.\ "}"; if (! CheckUtil.checkNulls (jscode,classify)) {/ / openid = OpenIdUtil.oauth3GetOpenid (jscode,classify);} return openid
1. Assemble OpenIdUtil with request parameters
Package com.wondersgroup.xa.healthyCard.user;/** * @ author xsx * / public class OpenIdUtil {public static String oauth3GetOpenid (String code,String classify) {String appid= "; String appsecret="; switch (classify) {case" jiankangka ": / / own configuration appid appid=" * "; / / own configuration APPSECRET Appsecret = "*"; break;} / / authorization (required) String grant_type = "authorization_code"; / / URL String requestUrl = "https://api.weixin.qq.com/sns/jscode2session";" / / request parameters String params = "appid=" + appid + "& secret=" + appsecret + "& js_code=" + code + "& grant_type=" + grant_type; / / send request String data = HttpUtil.get (requestUrl, params); return data;}}
2. HttpClient request tool class HttpUtil
Package com.wondersgroup.xa.healthyCard.user;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;import java.util.List;import java.util.Map / * * @ author xsx * / public class HttpUtil {/ * send the request for GET method to the specified URL * * @ param url * send the URL * @ param param * request parameters of the request, which should be in the form of name1=value1&name2=value2. The response result of the remote resource represented by * @ return String * / @ SuppressWarnings ("unused") public static String get (String url,String param) {String result = ""; BufferedReader in = null; try {String urlNameString = url + "?" + param; / / System.out.println (urlNameString); URL realUrl = new URL (urlNameString) / / Open the connection to URL URLConnection connection = realUrl.openConnection (); / / set the general request properties connection.setRequestProperty ("accept", "* / *"); connection.setRequestProperty ("connection", "Keep-Alive"); connection.setRequestProperty ("user-agent", "Mozilla/4.0 (compatible") MSIE 6.0; Windows NT 5.1: SV1) "); / / establish the actual connection connection.connect (); / / get all response header fields Map map = connection.getHeaderFields () / / iterate through all response header fields / * for (String key: map.keySet ()) {System.out.println (key + "-->" + map.get (key) } * / / define the BufferedReader input stream to read the URL response in = new BufferedReader (new InputStreamReader (connection.getInputStream (); String line; while ((line = in.readLine ())! = null) {result + = line }} catch (Exception e) {System.out.println ("an exception occurred sending a GET request!" + e); e.printStackTrace ();} / / use the finally block to close the input stream finally {try {if (in! = null) {in.close ();}} catch (Exception e2) {e2.printStackTrace () }} return result;}}
3. General class CheckUtil for program robustness verification
Package com.wondersgroup.xa.healthyCard.user;import org.apache.commons.lang.StringUtils;public class CheckUtil {/ * verifies whether there is a parameter with a null value. As long as there is one, it returns true * * @ param args * @ return true * / public static boolean checkNulls (String... Args) {if (args.length = = 0) {return true;} / / for (String str: args) {if (StringUtils.isEmpty (str)) {return true;}} return false;}} end
Copy the above utility class to your own service, and the call can be used normally.
I am Mo Jin, my friends often come to me to develop software, you can, Baidu Mo Jin and I get in touch
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.