微信開放平臺受權流程

最近在看微信第三方開發,整個流程看下來仍是能夠理解的json

https://blog.csdn.net/abc916545195/article/details/50977079?locationNum=16&fps=1api

借鑑了以上的博客緩存

整個流程坑很少,只是初看,有時有些不理解微信

 

首先,咱們要接收app

component_verify_ticket

POST數據示例

<xml>
<AppId> </AppId>
<CreateTime>1413192605 </CreateTime>
<InfoType> </InfoType>
<ComponentVerifyTicket> </ComponentVerifyTicket>
</xml>
字段說明
字段名稱 字段描述
AppId 第三方平臺appid
CreateTime 時間戳
InfoType component_verify_ticket
ComponentVerifyTicket Ticket內容

注意:
component_verify_ticket的有效時間較component_access_token更長,建議保存最近可用的component_verify_ticket,在component_access_token過時以前使用該ticket進行更新,避免出現由於ticket接收失敗而沒法更新component_access_token的狀況。this

 

建議使用https協議.net

public void doAuthorize(String nonce, String timestamp, String signature, String msgSignature, String xml) {
logger.info("[doAuthorize] nonce=" + nonce +
"; timestamp = "+ timestamp + " ;signature = " + signature +"; msgSignature= " + msgSignature + "; xml = "+ xml);
if (StringUtils.isEmpty(msgSignature)) {
return;
}
// 微信消息簽名驗證
boolean isValid = WechatOpenLogic.checkSignature(wechatToken, signature, timestamp, nonce);
if (!isValid){
logger.error("isValid false ; wechatToken " );
return;
}

Map<String, String> map = null;
//得到解密後的XML消息體
try {
map = WechatOpenLogic.decryptMsgToMap(wechatToken,encodingAesKey, componentAppId, msgSignature, timestamp, nonce, StringUtils.trim(xml));
}catch (Exception e){
logger.error("[doAuthorize] error =" + ExceptionUtils.getStackTrace(e));
return;
}
//消息類型
String infoType = map.get("InfoType");
switch (infoType) {
//推送ticket,用於獲取component_access_token
//這裏能夠獲取受權碼
case "component_verify_ticket":
String componentVerifyTicket = map.get("ComponentVerifyTicket");
storeWechatComponentVerifyTicket(componentVerifyTicket);
break;
// 刪除本地受權的公衆號
case "unauthorized":
    //移除緩存中的用戶信息,這裏是指取消的受權的用戶
this.unauthorized(map);
break;
////受權成功或更新受權成功
case "authorized":
case "updateauthorized":
    //獲取用戶信息並保存
    //這裏能夠獲取受權碼
            this.createOrUpdateAuthorized(map);
break;
}

}
第二步,獲取第三方平臺component_access_token
https請求:
https://api.weixin.qq.com/cgi-bin/component/api_component_token

 代碼以下3d

public String getComponentAccessToken() {
String cacheToke = stringCacheService.getCacheString(RedisSystemKey.COMPONENT_ACCESS_TOKEN);
if(!StringUtils.isBlank(cacheToke)){
return cacheToke;
}
String verifyTicket = stringCacheService.getCacheString(RedisSystemKey.COMPONENT_VERIFY_TICKET);
JSONObject jsonObject = new JSONObject();
jsonObject.put("component_appid", componentAppId);
jsonObject.put("component_appsecret", componentAppsecret);
jsonObject.put("component_verify_ticket", verifyTicket);
try {
HttpResponse response = HttpUtils.doPost(apiComponentTokenUrl,new HashMap<>(),new HashMap<>(), jsonObject.toJSONString());
String data = EntityUtils.toString(response.getEntity());
String token = JSONObject.parseObject(data).getString("component_access_token");
//設置過時時間,小於2小時
stringCacheService.storeCacheTimeOut(RedisSystemKey.COMPONENT_ACCESS_TOKEN, token, 6600, TimeUnit.SECONDS);
return token;
}catch (Exception e){
logger.error("[getComponentAccessToken] get token , error = " + ExceptionUtils.getStackTrace(e));
}
return null;
}

3:獲取預受權碼 pre_auth_code
http請求方式: POST(請使用https協議)https://api.weixin.qq.com/cgi-bin/component/api_create_preauthcode?component_access_token=xxx
相關文章
相關標籤/搜索