首页 > 开发 > Android > 正文

android 微信第三方登录怎么通过code获取openid?

2017-09-08 15:22:41  来源:网友分享

我已经获取到微信客户端返回的code,但是通过https://api.weixin.qq.com/sns/oauth2/access_toke网址没有获取到openid,网址无效果

解决方案

1.登录公众账号设置OAuth2.0
2.设置菜单按钮URL为OAuth链接
3.页面后台获取:

public String getopenId() {    HttpServletRequest request = ServletActionContext.getRequest();    HttpServletResponse response = ServletActionContext.getResponse();    response.setContentType("text/html");    String code = request.getParameter("code");    String urlstr = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=<appId>&secret=<secret>&code=" + code + "&grant_type=authorization_code";    JSONObject json;    try {        json = JSONObject.fromObject(HTTPTools.postToGetJson(urlstr));        openId = json.getString("openid");    } catch (Exception e) {        // e.printStackTrace();        return "";    }    return openId;}