微信支付java版本之獲取Access_token
access_token是公眾號的全局唯一票據(jù),公眾號調(diào)用各接口時都需使用access_token。開發(fā)者需要進(jìn)行妥善保存。access_token的存儲至少要保留512個字符空間。access_token的有效期目前為2個小時,需定時刷新,重復(fù)獲取將導(dǎo)致上次獲取的access_token失效。
公眾平臺的API調(diào)用所需的access_token的使用及生成方式說明:
1、為了保密appsecrect,第三方需要一個access_token獲取和刷新的中控服務(wù)器。而其他業(yè)務(wù)邏輯服務(wù)器所使用的access_token均來自于該中控服務(wù)器,不應(yīng)該各自去刷新,否則會造成access_token覆蓋而影響業(yè)務(wù);
2、目前access_token的有效期通過返回的expire_in來傳達(dá),目前是7200秒之內(nèi)的值。中控服務(wù)器需要根據(jù)這個有效時間提前去刷新新access_token。在刷新過程中,中控服務(wù)器對外輸出的依然是老access_token,此時公眾平臺后臺會保證在刷新短時間內(nèi),新老access_token都可用,這保證了第三方業(yè)務(wù)的平滑過渡;
3、access_token的有效時間可能會在未來有調(diào)整,所以中控服務(wù)器不僅需要內(nèi)部定時主動刷新,還需要提供被動刷新access_token的接口,這樣便于業(yè)務(wù)服務(wù)器在API調(diào)用獲知access_token已超時的情況下,可以觸發(fā)access_token的刷新流程。
如果第三方不使用中控服務(wù)器,而是選擇各個業(yè)務(wù)邏輯點各自去刷新access_token,那么就可能會產(chǎn)生沖突,導(dǎo)致服務(wù)不穩(wěn)定。
公眾號可以使用AppID和AppSecret調(diào)用本接口來獲取access_token。AppID和AppSecret可在微信公眾平臺官網(wǎng)-開發(fā)者中心頁中獲得(需要已經(jīng)成為開發(fā)者,且?guī)ぬ枦]有異常狀態(tài))。注意調(diào)用所有微信接口時均需使用https協(xié)議。
接口調(diào)用請求說明
http請求方式: GET
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
參數(shù)說明
返回說明
正常情況下,微信會返回下述JSON數(shù)據(jù)包給公眾號:
{"access_token":"ACCESS_TOKEN","expires_in":7200}
錯誤時微信會返回錯誤碼等信息,JSON數(shù)據(jù)包示例如下(該示例為AppID無效錯誤):
{"errcode":40013,"errmsg":"invalid appid"}
2.代碼實現(xiàn)
APPID,APPSECRET在公眾賬號中可查詢
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 { /** * 獲得ACCESS_TOKEN * * @Title: getAccess_token * @Description: 獲得ACCESS_TOKEN * @param @return 設(shè)定文件 * @return String 返回類型 * @throws */ @Autowired private AccessTokenService accessTokenServiceImpl; @Scheduled(fixedRateString = "${weixin.token.fixedRate.in.milliseconds}" , initialDelayString = "${weixin.token.initialDelay.in.milliseconds}") public void getAccessToken() { System.out.println("====================獲取token開始=============================="); 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"); // 必須是get方式請求 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("====================獲取token結(jié)束=============================="); is.close(); } catch (Exception e) { e.printStackTrace(); } // return accessToken; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析
這篇文章主要為大家介紹了微服務(wù)鏈路追蹤Spring Cloud Sleuth整合Zipkin解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02SpringBoot監(jiān)聽Nacos動態(tài)修改日志級別的操作方法
線上系統(tǒng)的日志級別一般都是 INFO 級別,有時候需要查看 WARN 級別的日志,所以需要動態(tài)修改日志級別,微服務(wù)項目中使用 Nacos 作為注冊中心,我們可以監(jiān)聽 Nacos 配置,修改日志級別,這篇文章主要介紹了SpringBoot監(jiān)聽Nacos動態(tài)修改日志級別的操作方法,需要的朋友可以參考下2023-12-12Java中常用的數(shù)據(jù)庫連接池_動力節(jié)點Java學(xué)院整理
數(shù)據(jù)庫連接池負(fù)責(zé)分配、管理和釋放數(shù)據(jù)庫連接,它允許應(yīng)用程序重復(fù)使用一個現(xiàn)有的數(shù)據(jù)庫連接,而不是再重新建立一個;釋放空閑時間超過最大空閑時間的數(shù)據(jù)庫連接來避免因為沒有釋放數(shù)據(jù)庫連接而引起的數(shù)據(jù)庫連接遺漏2017-08-08【spring-boot】快速構(gòu)建spring-boot微框架的方法
本篇文章主要介紹了【spring-boot】快速構(gòu)建spring-boot微框架的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解
這篇文章主要介紹了Spring中使用copyProperties方法進(jìn)行對象之間的屬性賦值詳解,使用org.springframework.beans.BeanUtils.copyProperties方法進(jìn)行對象之間屬性的賦值,避免通過get、set方法一個一個屬性的賦值,需要的朋友可以參考下2023-12-12