获取微信此详细授权的code

来源:互联网 发布:如何上传网站源码 编辑:程序博客网 时间:2024/06/13 16:37

1获取code(state=1)

 public void toWecharUserinfoCode(String state,HttpServletRequest request,HttpServletResponse response) throws IOException {
    String redirect_uri = getRedirectUrl("UTF-8",request);
    String appid = ServletContextUtil.getParamterValue("appid", "").toString();
    String wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appid+"&redirect_uri="+redirect_uri+"&response_type=code&scope=snsapi_userinfo&state="+state+"#wechat_redirect";
    response.sendRedirect(wxurl);
    }

2根据code 微信appid,secret可获取openid,accessToken

Map<String,String> tokenMap = getAccessToken(code,appid,secret);
openid = tokenMap.get("openid");
accessToken = tokenMap.get("accessToken");


 //通过微信客户端获取openid和access_token
 public Map<String,String> getAccessToken(String code,String appid,String secret){
Map<String,String>map = new HashMap<String,String>();
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
JSONObject demoJson = null;
InputStream is = null;
try{
URL urlGet = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
http.setRequestMethod("GET"); // 必须是get方式请求
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setDoOutput(true);
http.setDoInput(true);
System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
http.connect();

is = http.getInputStream();
int size = is.available();
byte[] jsonBytes = new byte[size];
is.read(jsonBytes);
String message = new String(jsonBytes, "UTF-8");
demoJson = new JSONObject(message);

//如果是错误则记录错误值
if(!demoJson.isNull("access_token")) {
map.put("accessToken", demoJson.getString("access_token"));
}
if(!demoJson.isNull("scope")) {
map.put("scope", demoJson.getString("scope"));
}
if(!demoJson.isNull("openid")) {
map.put("openid", demoJson.getString("openid"));
}

//如果是错误则记录错误值
if(!demoJson.isNull("errcode")) {
map.put("errcode", demoJson.getInt("errcode")+"" );
}

}catch(Exception e){
System.out.println(demoJson.toString());
printLog("getAccessToken异常 : code="+code+";appid="+appid+";secret="+secret+";demoJson="+demoJson.toString());
printLog(e);
} finally {
try {
if(is != null) {
is.close();
}
} catch (IOException e) {
printLog(e);
}
}
return map;
 }



//根据openid获取unionid

String at = GetUnionidBbyOpenId.getAccessToken();
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
+ at + "&openid=" + userModel.getOpenid() + "&lang=zh_CN";
JSONObject json = GetUnionidBbyOpenId.readJsonFromUrl(url);

String unionid=json.getString("unionid");


public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
   InputStream is = new URL(url).openStream();
   try {
     BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
     String jsonText = readAll(rd);
     JSONObject json = new JSONObject(jsonText);
     return json;
   } finally {
     is.close();
   }
}

阅读全文
0 0
原创粉丝点击