Micro-letter Payment Java version of the acquisition Access_token_java

Source: Internet
Author: User
Tags http request int size

Access_token is the public number's globally unique ticket, the public number calls each interface to use the Access_token. Developers need to be properly saved. Access_token storage should be reserved for at least 512 character spaces. The validity period of the Access_token is currently 2 hours and needs to be refreshed periodically, and repeated acquisition will cause the last acquired access_token to fail.

The use and generation of Access_token required by API calls to the public platform are described as follows:

1, in order to secure the Appsecrect, the third party needs a access_token to obtain and refresh the central control server. and other business logic server used by the Access_token are from the central control server, should not be each to refresh, otherwise it will cause access_token coverage and affect the business;
2, the current Access_token validity through the return of the expire_in to convey, is currently 7,200 seconds of value. The central control server needs to refresh the new Access_token in advance according to this effective time. In the refresh process, the output of the central control server is still the old Access_token, at this time the public platform backstage will ensure that in a short period of refresh, new and old Access_token are available, which ensures a smooth transition of the third party business;
3, Access_token effective time may be adjusted in the future, so the central control server not only need internal timing active refresh, also need to provide a passive refresh Access_token interface, so that business Server in the API call to know that Access_token has timed out , you can trigger the Access_token refresh process.

If the third party does not use the central control server, but chooses each business logic point each to refresh the Access_token, then may have the conflict, causes the service to be unstable.

The public number can use AppID and Appsecret to invoke this interface to obtain the Access_token. AppID and Appsecret are available on the website of the micro-credit public platform-Developer Center page (need to be a developer and the account is not in an abnormal state). Note The HTTPS protocol is required to invoke all micro-interfaces.

Interface Call Request Description
HTTP request mode: Get
Https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Parameter description

Return description


Normally, the micro-letter will return the following JSON packet to the public number:
{"Access_token": "Access_token", "expires_in": 7200}

In the case of an error, the micro-letter returns information such as the error code, and the JSON packet example is as follows (the example is an AppID invalid error):
{"Errcode": 40013, "errmsg": "Invalid AppID"}

2. Code implementation

Appid,appsecret can be queried in public accounts

Package com.zhrd.bussinss.platform.scheduled;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;

Import Java.net.URL;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.context.annotation.Lazy;
Import org.springframework.scheduling.annotation.Scheduled;

Import org.springframework.stereotype.Component;
Import Com.zhrd.bussinss.platform.constants.WeiXinId;

Import Com.zhrd.bussinss.platform.service.AccessTokenService;

Import Net.sf.json.JSONObject; @Component @Lazy (false) public class Getweixinaccesstokenscheduled {/** * obtained Access_token * * @Title: Getac
 
 Cess_token * @Description: Obtain Access_token * @param @return settings file * @return String return type * @throws * *
 
 
 @Autowired private Accesstokenservice Accesstokenserviceimpl; @Scheduled (fixedratestring = "${weixin.token.fixedrate.in.milliseconds}", initialdelaystring = "${
  Weixin.token.initialDelay.in.milliseconds} ") public void Getaccesstoken () {
  System.out.println ("==================== get token start =============================="); String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + weixinid.appid+

    "&secret=" + Weixinid.appsecret;
    String accesstoken = null;

    String expiresin = null;

      try {URL urlget = new URL (URL);

      HttpURLConnection http = (httpurlconnection) urlget.openconnection (); Http.setrequestmethod ("get");

      Must be get way request Http.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");

      Http.setdooutput (TRUE);

      Http.setdoinput (TRUE);

      Http.connect ();

      InputStream is = Http.getinputstream ();

      int size = is.available ();

      byte[] jsonbytes = new Byte[size];

      Is.read (jsonbytes);

      String message = new String (jsonbytes, "UTF-8");

      Jsonobject Demojson = jsonobject.fromobject (message);
      Accesstoken = demojson.getstring ("Access_token"); ExpireSIn = demojson.getstring ("expires_in");
      System.out.println ("accesstoken====" +accesstoken);
      System.out.println ("expiresin===" +expiresin);
      Accesstokenserviceimpl.addtoken (Accesstoken,expiresin);

      System.out.println ("==================== Get token end ==============================");

    Is.close ();

    catch (Exception e) {e.printstacktrace ();

  }//return accesstoken;

 }
 
}

  above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.