你好,看到您提问OAuth2.0网页授权微信怎么用java获取openid
首先需要在微信后台的网页授权那边添加你的信任的域名下地址
页面上获取code参数
function getQueryString(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return null;
}
var code = "";
$(function(){
code = getQueryString('code');
。。。。。
3.传入code调用接口获取openid
public static String oauth2GetOpenid(String code) {
HttpClient client = null;
String result = "";
try {
client = new DefaultHttpClient();
HttpGet httpget = new HttpGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+Constant.WECHATAPPID+"&secret="+Constant.WECHATAPPSECRET+"&code="+code+"&grant_type=authorization_code");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = client.execute(httpget, responseHandler);
Object obj = JSONValue.parse(response);
JSONObject jbt = (JSONObject) obj;
result = String.valueOf(jbt.get("openid"));
} catch (Exception e) {
e.printStackTrace();
} finally {
client.getConnectionManager().shutdown();
}
return result;
}