微信支付java版本之獲取Access_token
access_token是公眾號的全局唯一票據(jù),公眾號調(diào)用各接口時都需使用access_token。開發(fā)者需要進行妥善保存。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來傳達,目前是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)文章
idea2022創(chuàng)建javaweb項目步驟(超詳細)
本文主要介紹了idea2022創(chuàng)建javaweb項目步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java基于jdbc連接mysql數(shù)據(jù)庫功能實例詳解
這篇文章主要介紹了java基于jdbc連接mysql數(shù)據(jù)庫功能,結(jié)合實例形式詳細分析了jdbc連接mysql數(shù)據(jù)庫的原理、步驟、實現(xiàn)方法及相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Java String字符串和Unicode字符相互轉(zhuǎn)換代碼詳解
這篇文章主要介紹了Java String字符串和Unicode字符相互轉(zhuǎn)換代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

