Java 微信获取临时素材对接代码

1.微信文档地址

https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Get_temporary_materials.html

2.代码

如果返回的是视频消息素材,则返回的貌似是一个json,那样的话就不适合使用以下代码

我是用来获取微信录音的,获取到文件流之后还需要使用 音频转码工具 AudioUtils.amrToMp3 转为mp3

	/**
     * 获取临时素材
     * @author hcx
     * @since 2024-01-03
     * @return String
     */
	public InputStream getTemporaryMaterial(String mediaId) throws IOException {
        // 请求微信临时素材接口
        String token = "access_token";
        String url = "https://api.weixin.qq.com/cgi-bin/media/get?" + token + "=ACCESS_TOKEN&media_id=" + mediaId;
        // 请求参数
        HashMap<String, String> param = new HashMap<>();
        param.put("media_id",mediaId);
        // 获取到http连接对象
        HttpPost httpPost = new HttpPost(url);
        StringEntity stringEntity = new StringEntity(JSONObject.toJSONString(param));
        httpPost.setEntity(stringEntity);
        // 打开链接发送请求 获取到返回的流
        CloseableHttpClient build = HttpClients.custom().build();
        CloseableHttpResponse execute = build.execute(httpPost);
        InputStream inputStream = execute.getEntity().getContent();
        return inputStream;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值