微信小程序怎么获取openid

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了微信小程序怎么获取openid相关的知识,希望对你有一定的参考价值。

参考技术A 登陆小程序的网页后台,在基础资料里面可以看到,要用管理微信进行验证才会出现。 参考技术B 在微信开发项目中,获取openid是项目常遇的问题,本文通过主要讲解实现在微信小程序中如何获取用户的openid,案例实现非常简单

微信小程序,weixin,怎么获取用户的Openid

参考技术A 获取微信OpenId 先获取code 再通过code获取authtoken,从authtoken中取出openid给前台 微信端一定不要忘记设定网页账号中的授权回调页面域名 流程图如下 主要代码 页面js代码 /* 写cookie */ function setCookie(name, value) var Days = 30; var exp = new Date(); exp/connect/oauth2/authorize?appid=") /sns/oauth2/access_token?"; StringBuffer url = new StringBuffer(uri); url.URL; public class HttpClientUtil // 设置body体 public static void setBodyParameter(String sb, HttpURLConnection conn) throws IOException DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(sb); out.flush(); out.close(); // 添加签名header public static HttpURLConnection CreatePostHttpConnection(String uri) throws MalformedURLException, IOException, ProtocolException URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setInstanceFollowRedirects(true); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setRequestProperty("Content-Type","application/json"); conn.setRequestProperty("Accept-Charset", "utf-8"); conn.setRequestProperty("contentType", "utf-8"); return conn; public static byte[] readInputStream(InputStream inStream) throws Exception ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) outStream.write(buffer, 0, len); byte[] data = outStream.toByteArray(); outStream.close(); inStream.close(); return data; 封装AuthToken的VO类 package com.huatek.shebao.wxpay; public class AuthToken private String access_token; private Long expires_in; private String refresh_token; private String openid; private String scope; private String unionid; private Long errcode; private String errmsg; public String getAccess_token() return access_token; public void setAccess_token(String access_token) this.access_token = access_token; public Long getExpires_in() return expires_in; public void setExpires_in(Long expires_in) this.expires_in = expires_in; public String getRefresh_token() return refresh_token; public void setRefresh_token(String refresh_token) this.refresh_token = refresh_token; public String getOpenid() return openid; public void setOpenid(String openid) this.openid = openid; public String getScope() return scope; public void setScope(String scope) this.scope = scope; public String getUnionid() return unionid; public void setUnionid(String unionid) this.unionid = unionid; public Long getErrcode() return errcode; public void setErrcode(Long errcode) this.errcode = errcode; public String getErrmsg() return errmsg; public void setErrmsg(String errmsg) this.errmsg = errmsg; 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上是关于微信小程序怎么获取openid的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序如何开发,怎么获取openID和用户信息

微信小程序怎么获取openid

微信小程序:获取openid失败原因

为啥别人访问我微信小程序获取不到openID

如何获取微信小程序用户openid

微信小程序登录获取openid有时候为啥第一次会失败,第二次才成功?