背景:获取已有微信小程序的码(小程序码/二维码),保存为图片上传至cdn服务器,供使用方自行下载使用。
查看微信小程序官方文档,获取小程序码分两个步骤,调用接口如下:
1、获取access_token
2、根据上面获取的access_token,请求小程序码:
上面是直接用postman请求的,下面看用Java程序获取:
首先看下设计思路:先将获取到的二进制流保存到本地图片,然后将图片文件上传到cdn上,就这么简单。(代码只展示了获取有限的永久小程序码)
package com.integration.wechat.impl;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springfr