In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to get access_token on Wechat official account". The content is simple and clear. I hope it can help you solve your doubts. Now let the editor lead you to study and learn how to get access_token on Wechat official account.
Access_token is the only global API call credential of the official account. We interact with the Wechat server, and the server uses access_token to determine who we are (the request of which official account service). Therefore, the access_token we get from the server in the development process must not be explicitly exposed to the outside world, otherwise it will lead to data security problems. Someone else gets your accessToken and operates your official account. The validity period of access_token is currently 2 hours. You need to obtain it again when it expires.
Here is a way to get access_token
1. Add httpclient-related dependencies to the project. The example uses httpclient to request Wechat server to get the result returned by Wechat.
Org.apache.httpcomponents httpclient 4.5.3 org.apache.httpcomponents httpcore 4.4.6
2.httpClientUtil class, easily found on the Internet. Try the doget method in this example. There is no problem. The rest will not be considered for the time being.
Public class HttpClientUtil {public static String doGet (String url, Map param) {/ / create Httpclient object CloseableHttpClient httpclient = HttpClients.createDefault (); String resultString = ""; CloseableHttpResponse response = null; try {/ / create uri URIBuilder builder = new URIBuilder (url); if (param! = null) {for (String key: param.keySet ()) {builder.addParameter (key, param.get (key));}} URI uri = builder.build (); / / create http GET request HttpGet httpGet = new HttpGet (uri) / / execute request response = httpclient.execute (httpGet); / / determine whether the returned status is 200if (response.getStatusLine (). GetStatusCode () = = 200) {resultString = EntityUtils.toString (response.getEntity (), "UTF-8");}} catch (Exception e) {e.printStackTrace ();} finally {try {if (response! = null) {response.close ();} httpclient.close ();} catch (IOException) {e.printStackTrace () } return resultString;} public static String doGet (String url) {return doGet (url, null);} public static String doPost (String url, Map param) {/ / create Httpclient object CloseableHttpClient httpClient = HttpClients.createDefault (); CloseableHttpResponse response = null; String resultString = ""; try {/ / create HttpPost request HttpPost httpPost = new HttpPost (url); / / create parameter list if (param! = null) {List paramList = new ArrayList () For (String key: param.keySet ()) {paramList.add (new BasicNameValuePair (key, param.get (key);} / / Simulation form UrlEncodedFormEntity entity = new UrlEncodedFormEntity (paramList, "utf-8"); httpPost.setEntity (entity);} / / execute http request response = httpClient.execute (httpPost); resultString = EntityUtils.toString (response.getEntity (), "utf-8");} catch (Exception e) {e.printStackTrace ();} finally {try {response.close () } catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();} return resultString;} public static String doPost (String url) {return doPost (url, null);} public static String doPostJson (String url, String json) {/ / create Httpclient object CloseableHttpClient httpClient = HttpClients.createDefault (); CloseableHttpResponse response = null; String resultString = ""; try {/ / create HttpPost request HttpPost httpPost = new HttpPost (url) / / create request content StringEntity entity = new StringEntity (json, ContentType.APPLICATION_JSON); httpPost.setEntity (entity); / / execute http request response = httpClient.execute (httpPost); resultString = EntityUtils.toString (response.getEntity (), "utf-8");} catch (Exception e) {e.printStackTrace ();} finally {try {response.close ();} catch (IOException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} return resultString;}}
3. The third step is to simply test the code.
Public class WeChatAccessTokenTest {public static void main (String [] args) {Map params = new HashMap (); / / TODO: 2018-11-16 * changed to real appid params.put ("appid", "*"); / / TODO: 2018-11-16 * changed to real secret params.put ("secret", "*"); params.put ("grant_type", "client_credential") String response = HttpClientUtil.doGet ("https://api.weixin.qq.com/cgi-bin/token", params); JSONObject accessTokenObject = JSONObject.parseObject (response); String accessToken = accessTokenObject.getString (" access_token "); Long expire = accessTokenObject.getLong (" expires_in "); System.out.println (accessToken);}}
The above is all the contents of the article "how to get access_token on Wechat official account". 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.