企业微信更换头像
步骤:
1、appid、应用secret
2、获取access_token
3、根据access_token调用企微上传媒体库接口
4、根据人员标识字段更换人员头像
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.utils.IOUtils;
import com.mehow.hklink.service.impl.QywxLinkServiceImpl;
import com.mehow.hklink.utils.maycur.HttpRequest;
import org.apache.commons.codec.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
public class WxLinkUtil {
final static String APPID="xxxxxxxx";
/**
* 企业微信应用secret
*/
final static String ORGSECRT="xxxxxxxx";
private static Logger log = LoggerFactory.getLogger(WxLinkUtil.class);
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
/**
* AccessToken获取
* @param
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws KeyManagementException
*/
public static JSONObject getOrgAccessToken() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException{
// 微信secret 业务
//String requestTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+APPID+"&corpsecret="+ORGSECRT+"";
// requestTokenUrl = requestTokenUrl.replace("APPID", APPID);
// requestTokenUrl = requestTokenUrl.replace("SECRET", ORGSECRT);
StringBuffer jsonObjectToken = CommonHttpUtil.httpsRequest("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+APPID+"&corpsecret="+ORGSECRT+"", "GET", null);
// JSONObject jsonObject = JSONObject.parseObject(jsonObjectToken.toString());
// System.out.println("---jsonObject-->>"+jsonObject);
return JSONObject.parseObject(jsonObjectToken.toString());
}
/**
* 上传临时素材
* 素材上传得到media_id,该media_id仅三天内有效
* media_id在同一企业内应用之间可以共享
* 图片(image):10MB,支持JPG,PNG格式
* 语音(voice) :2MB,播放长度不超过60s,仅支持AMR格式
* 视频(video) :10MB,支持MP4格式
* 普通文件(file):20MB
* @param access_token
* @param file 需要上传的文件
* @param type 文件类型
* @throws IOException
*/
public static String uploadMedia(String access_token , File file, String type,String params) throws IOException {
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
url = url.replace("ACCESS_TOKEN",access_token).