shortlink.generate 微信小程序分享
【官方文档】:shortlink.generate https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/short-link/shortlink.generate.html
需求:
获取小程序 Short Link 方法
@Value("${wxmini.appId:}") private String appId; @Value("${wxmini.appSecret:}") private String appSecret; /** * 微信API服务 */ private WxMaService wxMaService; @PostConstruct public void init() { WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(appId); config.setSecret(appSecret); wxMaService = new WxMaServiceImpl(); wxMaService.setWxMaConfig(config); } /** * 获取小程序 Short Link */ public String getShortLink(String pageUrl, String pageTitle, boolean isPermanent) throws Exception { try { JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("page_url", pageUrl); jsonObject.addProperty("page_title", pageTitle); jsonObject.addProperty("is_permanent", isPermanent); String res = this.wxMaService.post("https://api.weixin.qq.com/wxa/genwxashortlink", jsonObject.toString()); if (StringUtils.isBlank(res)) { throw new Exception("获取小程序 Short Link异常"); } JSONObject obj = JSONObject.parseObject(res); return obj.getString("link"); } catch (Exception e) { log.error("获取小程序 Short Link异常,msg=" + e.getMessage(), e); throw new Exception("获取小程序 Short Link异常"); } }
配置: 版本比较老,自行升级
# 微信小程序 配置wxmini.appId=wxmini.appSecret=
踩过的坑:
"errcode":40066,"errmsg":"invalid url"
url 需要是发布的小程序有的 url 。 不是指的请求 url “https://api.weixin.qq.com/wxa/genwxashortlink”, 而是参数 “page_url”
https://developers.weixin.qq.com/community/develop/doc/000c685e7b4968c7a08d92a8a51c00?highLine=shortlink.generate
这篇关于shortlink.generate 微信小程序分享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持
还没有评论,来说两句吧...