获取微信此详细授权的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();
}
}
- 获取微信此详细授权的code
- 微信授权获取code是的值得注意的地方。
- 微信网页授权获取code值回调两次的问题
- 【微信】网页授权,获取CODE,OPENID,
- 微信授权code在tp框架下获取不到
- 微信网页授权,获取微信code,获取access_tocken,获取用户信息
- 微信授权获取用户的openid
- java 网页授权获取微信用户信息错误 40029 微信端会发出两次请求 错误的oauth第二次无效的code
- 微信授权获取用户信息
- 微信授权获取基本信息
- 微信oauth2网页授权code过期
- 微信登录的code获取
- 关于微信第三方授权登陆 使用code获取token时返回40029
- 获取新浪微博授权之后返回的code
- 自动获取微博的授权code码
- 微信授权登录获取用户信息的步骤
- 微信开发获取code
- 微信授权方法及通过授权获取临时访客的个人信息方法
- HDU 6129 规律+数论
- oracle数据库常用操作命令
- 一些常用命令
- angular2系列教程(九)Jsonp、URLSearchParams、中断选择数据流
- android实现炫酷水波纹界面
- 获取微信此详细授权的code
- 第二十二篇:并发新特性—障碍器CyclicBarrier(含代码)
- Keras搭建的自编码模型
- 表示“场所”的に、で的区别
- PL/SQL在Oracle服务器上连接出错
- 前端面试题(部分)
- 关于Struts2项目运行报错Unable to load configuration
- 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
- codeforces 845B Luba And The Ticket