微信oath2授权获取openid

[1].[代码]获取授权签名链接

function seturl(url){

varnewUrl = encodeURIComponent(url);

varreNewUrl ='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 

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();

}

returnopenid;

}


原文 http://www.oschina.net/code/snippet_2255303_46155

你可能感兴趣的:(微信oath2授权获取openid)