注册

微信oath2授权获取openid

经常碰到有需要链接获取用户openid的直接给代码 照例jfinal
1.开发者中心-->网页授权获取用户基本信息-->授权回调页面域名
2.调用链接转换
3. 获取code 用code 换取openid


1. 获取授权签名链接
function seturl(url){
var newUrl = encodeURIComponent(url);
var reNewUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=${(AppId)!}&redirect_uri=' + newUrl + '&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
}

2. code换取openid 照例jfinal
public String getopendid(String code) {
String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=AppId&secret=AppSecret&code=CODE&grant_type=authorization_code";
url = url.replace("AppId", AppId).replace("AppSecret", AppSecret)
.replace("CODE", code);
String jsonResult = HttpKit.get(url);
JSONObject jsonTexts = (JSONObject) JSON.parse(jsonResult);
String openid = "";
if (StringKit.notNull(jsonTexts.get("openid"))) {
openid = jsonTexts.get("openid").toString();
}

return openid;
}

已邀请:

要回复问题请先登录注册