2025年获取微信 access_token,ticket,signature

获取微信 access_token,ticket,signature代码记录 package com yunxi access action import com alibaba fastjson JSONObject import com yunxi access constant config RedisCache import com yunxi util common RandomCreate import

大家好,我是讯享网,很高兴认识大家。

代码记录:


讯享网

package com.yunxi.access.action; import com.alibaba.fastjson.JSONObject; import com.yunxi.access.constant.config.RedisCache; import com.yunxi.util.common.RandomCreateCode; import com.yunxi.util.http.HttpPostUtil; import com.yunxi.util.security.Sha1; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import java.net.URLDecoder; import static com.yunxi.util.common.RandomCreateCode.MODE_CHARACTER; / * 微信js分享 * 获取 access_token,ticket,signature * @Author QZJ * @CreateTime 2018/12/20 */ @RestController public class WxAction { @Value("${wx.appId}") String appId; @Value("${wx.appSecret}") String appSecret; @Autowired RedisCache redisCache; private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; private static final String JS_TICKET = "JS_TICKET"; private static final int EXPIRE_TIME = 120 * 60; @ApiOperation(value = "获取微信分享信息", notes = "JSON") @PostMapping("/getWxInfo") public @ResponseBody JSONObject getWxInfo(@RequestParam(value = "shareLink") String shareLink) throws Exception { // 获取token String accessToken = getValue("https://api.weixin..com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret, "access_token", ACCESS_TOKEN); // 获取 ticket 注意链接前端需要encode,因为包含连接符 String ticket = getValue("https://api.weixin..com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi", "ticket", JS_TICKET); // 生成签名 return getSignature(ticket, shareLink); } / * 生成签名信息 * @param ticket * @param shareLink * @return * @throws Exception */ private JSONObject getSignature(String ticket, String shareLink) throws Exception { JSONObject wxObject = new JSONObject(); Long timestamp = System.currentTimeMillis() / 1000; String nonceStr = RandomCreateCode.randomNumber(10, MODE_CHARACTER); String param = "jsapi_ticket=" + ticket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + URLDecoder.decode(shareLink, "utf-8"); wxObject.put("timestamp", timestamp); wxObject.put("nonceStr", nonceStr); wxObject.put("signature", Sha1.encode(param)); return wxObject; } / * 取微信值 并 存储 redis * * @param url * @param key * @param redisKey * @return * @throws Exception */ private String getValue(String url, String key, String redisKey) throws Exception { // 从redis中取值 String value = redisCache.getValue(redisKey); if (!StringUtils.isEmpty(value)) { return value; } // 微信取值 String result = HttpPostUtil.sendPost(url, "", HttpPostUtil.FORM); value = JSONObject.parseObject(result).getString(key); // 存储 redisCache.setValue(redisKey, value, EXPIRE_TIME); return value; } }

讯享网
小讯
上一篇 2025-04-07 11:56
下一篇 2025-01-10 16:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/135149.html