微信网页授权:scope有两种方式:
scope=snsapi_userinfo:需要用户点击确认,才会去登录
scope=snsapi_base:不需要用户点击确认,默认登录
var callbackUrl ="http://www.xxx.com";
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=' +
encodeURI(callbackUrl) + '&response_type=code'+'&scope=snsapi_userinfo#wechat_redirect';
window.location.href = url;
var callbackUrl = 'http://www.xxx.com';
var url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=xxx&redirect_uri=' +
encodeURI(callbackUrl) + '&response_type=code'+'&scope=snsapi_base#wechat_redirect';
window.location.href = url;
两种方式,都可以拿到code,再通过code,获取token和openid
private String getWxToken(String code) throws IOException {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+WxPayParam.APPID +
"&secret="+WxPayParam.APPSECRET+"&code=" + code + "&grant_type=authorization_code";
PostMethod post = new PostMethod(url);
HttpClient client = new HttpClient();
client.executeMethod(post);
String response = post.getResponseBodyAsString();
logger.info("response: " + response);
post.releaseConnection();
return response;
}
{
"access_token":"ACCESS_TOKEN",
"expires_in":7200,
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID",
"scope":"SCOPE"
}
public class JsonUtils {
/**
* json字符串转成map类型数据
*/
public static Map
return new GsonBuilder().create().fromJson(jsonString, new TypeToken
}
/**
* json字符串转成map对象
* @param jsonString
* @return
*/
public static Map
return new GsonBuilder().create().fromJson(jsonString, new TypeToken
}
}
String token = JsonUtils.jsonStringToMap(obj).get("access_token");
String openid = JsonUtils.jsonStringToMap(obj).get("openid");
String userInfo = getGzhUserInfo(token, openid);
private String getGzhUserInfo(String token, String openid) {
try {
String url ="https://api.weixin.qq.com/sns/userinfo?access_token="+token+"&openid="+openid+"&lang=zh_CN";
GetMethod get = new GetMethod(url);
HttpClient client = new HttpClient();
client.executeMethod(get);
String userInfo = get.getResponseBodyAsString();
get.releaseConnection();
userInfo = new String(userInfo.getBytes("iso-8859-1"), "utf-8").
replace("[", "\"").replace("]", "\"");
return userInfo;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
还有一个获取用户信息的接口:
https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
(此接口的access_token 是接口基础调用access_token 不是网页授权access_token),只有注了公众号后,才能调用成功
基础接口的token 获取接口是
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
用户网页授权access_token 获取接口地址是
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
{
"openid":" OPENID",
" nickname": NICKNAME,
"sex":"1",
"province":"PROVINCE"
"city":"CITY",
"country":"COUNTRY",
"headimgurl": "http://thirdwx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46",
"privilege":[ "PRIVILEGE1" "PRIVILEGE2" ],
"unionid": "o6_bmasdasdsad6_2sgVt7hMZOPfL"
}
更多详细内容请查看官网:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842