shortlink.generate 微信小程序分享

2022/2/23 20:24:06

本文主要是介绍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异常");
        }
    }

配置:
版本比较老,自行升级

<properties>
    <java.version>1.8</java.version>
    <binarywang.version>3.9.6.B</binarywang.version>
<!--    <binarywang.version>4.1.8.B</binarywang.version> -->
</properties>

<!-- 微信开放平台(包含小程序、app、公众号) -->
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-open</artifactId>
    <version>${binarywang.version}</version>
</dependency>
# 微信小程序   配置
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 微信小程序分享的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程