最近写小程序,服务器端使用的Java,要使用微信用户的Openid,研究了一下,找到了一个简单的获取方法,经过测试以后成功获取了微信Openid。
注意:测试的时候每次code都需要更新。
protected static String getWXOpenid(String code) {
String appid = "此处请填写你的小程序的appid";
String secret = "此处请填写你的小程序的secret key";
String url="https://api.weixin.qq.com/sns/jscode2session?appid="
+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
StringBuffer sb = new StringBuffer();
sb.append("");
If (code != null)
try {
URL weChatUrl = new URL(url);
URLConnection connection = weChatUrl.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
sb.append(line);
}
} catch (Exception e) {
}
return sb.toString();
}
1115

被折叠的 条评论
为什么被折叠?



