如何获取微信openId 您所在的位置:网站首页 微信openid获取失败是什么意思啊怎么办 如何获取微信openId

如何获取微信openId

2024-07-12 01:21| 来源: 网络整理| 查看: 265

获取微信openid openid定义

为了识别用户,每个用户针对每个公众号会产生一个安全的OpenID,如果需要在多公众号、移动应用之间做用户共通,则需前往微信开放平台,将这些公众号和应用绑定到一个开放平台账号下,绑定后,一个用户虽然对多个公众号和应用有多个不同的OpenID。所以一个微信号在一个公众号下的openid是不变的,如果换了一个对应的公众号,那就是另一个openid了。且只有在微信自带浏览器中打开的项目才可获取到。

准备条件

因为一个openid对应一个微信用户一个公众号,所以首先你要有一个公众号,还有一个外网可访问的域名,我的公众号类型是企业号,这里就以企业号为例。

获取openid需要的公众号的 appid 和 secret(登陆公众平台 开发----->基本配置中的开发者ID(AppID)和 开发者密码(AppSecret)就是)。

5b2ca57c0c12b 5b2ca57c0c12b

其次是设置网页授权域名(登陆公众平台 设置----->公众号设置------>功能设置----->网页授权域名 按步骤操作并设置就好),这个域名就是你获取openid的web项目发布的域名,这里注意服务器请一定跑在80端口。

基本流程:

openid作为用户信息的一部分,要获取到需要调用微信两个开放授权接口,接口遵循OAuth 2.0协议。开放授权标准允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用,也就是我们常用的微信登录、微博登录等等,英文好的同学可以看看OAuth 2.0 Authorization Framework。

我整理了一下整体的流程,画了一个时序图。

image.png

其中两次调用了微信嗯验证服务器。

调用https://open.weixin.qq.com/connect/oauth2/authorize 接口获取到code,注意这个接口只获取openid,scope参数设置为snsapi_base,这样不需要用户确认和关注公众号。

不知道你有没有疑问,为什么不直接获取openId,还需要获取一个code呢?

是因为code相当于一个临时票据,能够验证失效时间、可获取信息的内容、微信用户、appid等等。

得到code作为一个参数,加上appId和appsecret,调用https://api.weixin.qq.com/sns/oauth2/access_token 接口获取到openid。

注意,appsecret是比较重要的参数要放到后台进行请求。返回的重要参数又openId和access_token,用openId和access_token可以获取用户的基本信息,位置性别等等等,我们这里只讲获取openId,原理类似,想看参考微信网页授权。

代码 前端 function openId(){ //测试 var callbackUrl = link.skip + itemId; var appid = link.appid; var redirect_uri = encodeURI(callbackUrl); var code = requestUtil.getParameter('code'); if (code && localStorage.getItem('code') != code) { //微信回调含code调用免登陆接口,否则跳转微信验证 $.ajax({ type: 'get', url: link.path+"/v1/wechatUser/info?code=" + code, async: false, success: function (res) { if(res.code == 1000) { localStorage.setItem('code', code); var openId = res.data.openId; localStorage.setItem('openId', openId); } } }); } else { window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + redirect_uri + '&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'; } } 后端 public void getOpenId(HttpServletRequest request, HttpServletResponse response,String code) throws UnsupportedEncodingException { response.setContentType("text/html"); request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); Map params = new HashMap(); params.put("secret", ""); params.put("appid", ""); params.put("grant_type", "authorization_code"); params.put("code", code); String result = HttpGetUtil.httpRequestToString( "https://api.weixin.qq.com/sns/oauth2/access_token", params); JSONObject jsonObject = JSONObject.parseObject(result); String openid = jsonObject.get("openid").toString(); System.out.println("得到的openid为:"+openid); } static class HttpGetUtil { public static String httpRequestToString(String url, Map params) { String result = null; try { InputStream is = httpRequestToStream(url, params); BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } result = buffer.toString(); } catch (Exception e) { return null; } return result; } private static InputStream httpRequestToStream(String url, Map params) { InputStream is = null; try { String parameters = ""; boolean hasParams = false; for(String key : params.keySet()){ String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key +"="+ value +"&"; hasParams = true; } if(hasParams){ parameters = parameters.substring(0, parameters.length()-1); } url += "?"+ parameters; URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Accept-Charset", "UTF-8"); conn.setRequestProperty("contentType", "utf-8"); conn.setConnectTimeout(50000); conn.setReadTimeout(50000); conn.setDoInput(true); //设置请求方式,默认为GET conn.setRequestMethod("GET"); is = conn.getInputStream(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return is; } } 问题补充 authorize接口: authorize接口,只能通过微信,或者微信开发这工具打开(需要加开发权限)。 access_token接口: 如果返回40163错误,是因为code已经被用过,code只有一次生命,用完作废。如果出现了code请检查一下接口1相关的代码,是不是对code进行了保存等等。 感谢

感谢各位看完这篇文章,如果有问题或不清楚直接留言,或者加vx34108314。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有