使用media_id获取素材提示40007,不合法的媒体文件 id?
发布于 4 年前 作者 wumin 8573 次浏览 来自 问答
 我使用微信公众号在图文素材的内容中上传了一个视频,微信返回一个media_id,我想通过这个media_id调用一下接口
 https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN
获取该视频素材(获取可以直接访问服务器上该视频的url,图片是可以的),但是返回结果显40007,不合法的媒体文件 id
代码如下:
String accessToken = wxMpService.getAccessToken();
System.out.println("获取url的token:"+accessToken);
String path = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
path = path.replace("ACCESS_TOKEN",accessToken);
JSONObject json = new JSONObject();
json.put("media_id",mediaId);
String result = FileUtil.HttpPostWithJson(path, json.toJSONString());
System.out.println("result:"+result);
return result;

public static String HttpPostWithJson(String url, String json) {
    String returnValue = "这是默认返回值,接口调用失败";
    CloseableHttpClient httpClient = HttpClients.createDefault();
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try{
        //第一步:创建HttpClient对象
        httpClient = HttpClients.createDefault();
        //第二步:创建httpPost对象
        HttpPost httpPost = new HttpPost(url);
        //第三步:给httpPost设置JSON格式的参数
        StringEntity requestEntity = new StringEntity(json,"utf-8");
        requestEntity.setContentEncoding("UTF-8");
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setEntity(requestEntity);
        //第四步:发送HttpPost请求,获取返回值
        returnValue = httpClient.execute(httpPost,responseHandler); //调接口获取返回值时,必须用此方法
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally {
        try {
            httpClient.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    //第五步:处理返回值
    return returnValue;
}
回到顶部