JAvaマイクロメッセージウィジェットユーザー情報の取得
/**
* openid session_key/unionid
* :{"session_key":"ZXxmVGt78993dkNCPmQOA==","openid":"o5TI441P58849p4E-fBrH_HgVvGo","unionid":"oD08m1s24646ZIDJ_AQ9ADUf5QQ"}
* @param code
* @return
* @throws Exception
* @return JSONObject
* @author tyg
* @date 2019 4 25 2:24:22
*/
public static JSONObject getWechatSmallProgram(String code) throws Exception {
// ( )
String wxspAppid = "wx456478dfsffdsfsdfsa";
// app secret ( )
String wxspSecret = "4ds4f89d4afa46fsfsd";
//
String url = "https://api.weixin.qq.com/sns/jscode2session?";
// ( )
String grant_type = "authorization_code";
//1、 code session_key openid
//
StringBuffer params = new StringBuffer("appid=").append(wxspAppid);
params.append("&secret=").append(wxspSecret);
params.append("&js_code=").append(code);
params.append("&grant_type=").append(grant_type);
String data = HttpTookit.doGet(url, params.toString(), "UTF-8", true);
LOG.info("
:"+data);
return JSONObject.parseObject(data);
}
ステップ2:session_の使用keyはencryptedDataを復号し,ユーザ情報を取得し,ユーザニックネーム,アバター情報を返す.
package com.yt.distribution.util;
import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* Created by yfs on 2017/2/6.
*
* AES-128-CBC
* :
* AES-128-CBC “ ” “ “。
* AES-128 jdk “ ”。
*/
public class AesCbcUtil {
static {
//BouncyCastle , http://www.bouncycastle.org/
Security.addProvider(new BouncyCastleProvider());
}
/**
* AES
*
* @param data // ,
* @param key //
* @param iv //
* @param encodingFormat //
* @return
* @throws Exception
*/
public static String decrypt(String data, String key, String iv, String encodingFormat) throws Exception {
// initialize();
//
byte[] dataByte = Base64.decodeBase64(data);
//
byte[] keyByte = Base64.decodeBase64(key);
//
byte[] ivByte = Base64.decodeBase64(iv);
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
SecretKeySpec spec = new SecretKeySpec(keyByte, "AES");
AlgorithmParameters parameters = AlgorithmParameters.getInstance("AES");
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);//
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, encodingFormat);
return result;
}
return null;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
}