微信内的webview中获取微信用户的基本信息(open_id)
来源:互联网 发布:二手跳蚤市场淘宝 编辑:程序博客网 时间:2024/05/16 10:19
前提:
已经开启高级接口的微信公众号(服务号为优)
微信的oauth
(其他方式都行不通:url参数太危险,session不能被保存,event=view类型的事件不能动态改变key(url)等等。。。)
过程:
应用授权作用域,snsapi_base
http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html
1. 设置自定义菜单链接地址
类似:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx*********&redirect_uri=http%3A%2F%2Fwx.edaysoft.cn%2Fhome%2Ftest&response_type=code&scope=snsapi_base&state=yunnying#wechat_redirect
2. redirect_uri获取code和state
3. HttpWebRequest获取openid和token
4.结束。或是其他想要深入的东西
//redirect_uri的地址public ActionResult Test(){ string code = Request.QueryString["code"]; string state = Request.QueryString["state"]; string msg = ""; if (string.IsNullOrEmpty(code)) { msg = "用户禁止授权"; } string url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + "wx********************" + "&secret=" + "SSSSSSSSSSSSSSSSSSSSSSSS" + "&code=" + code + "&grant_type=authorization_code"; HttpWebResponse response = this.GetRestfulResponse(url); if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string responseString = sr.ReadToEnd(); wxopenidjsonobject obj = JsonConvert.DeserializeObject<wxopenidjsonobject>(responseString); msg = obj.openid; } } if (string.IsNullOrWhiteSpace(msg)) { msg = "sth wrong."; } ViewBag.from = msg; return View();}private HttpWebResponse GetRestfulResponse(string url, int timeout = 600000){ HttpWebRequest request = WebRequest.CreateHttp(url); request.ContentType = "application/json;charset=utf-8"; request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.Default); request.Method = "GET"; request.ContentLength = 0; request.Timeout = timeout; return request.GetResponse() as HttpWebResponse;}public class wxopenidjsonobject{ public string access_token { get; set; } public int expires_in { get; set; } public string refresh_token { get; set; } public string openid { get; set; } public string scope { get; set; }}
注意事项:
1. redirect_uri可以为任意的,不同于类似微盟、点点客之类的微信托管平台,自己host就可以
2. 设置 微信公众平台 里的 开发者中心 中 网页服务 之 网页授权获取用户基本信息 的 授权回调页面域名,以免报错:redirect_uri 参数错误
0 0
- 微信内的webview中获取微信用户的基本信息(open_id)
- PHP如何获取微信用户的 openid 和基本信息
- 微信 获取用户基本信息
- 微信获取用户基本信息
- 微信获取用户基本信息
- 微信公众号PHP开发怎么获取用户的基本信息(姓名、性别、年龄等)
- 微信开发(1) :网页授权获取用户的基本信息 实现微信登录(java)
- php 获取 微信 open_id 部分代码
- 微信开发一(获取用户基本信息)
- 微信开发笔记-(2)获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信高级接口获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信网页授权获取用户基本信息
- 微信批量获取用户基本信息接口
- 微信开发 获取用户基本信息 php
- 微信授权php获取用户基本信息
- 微信开发获取用户基本信息
- python 学习笔记之 全局变量和局部变量
- windows svn install service
- 几种常见加密算法初窥及如何选用加密算法(AES,DES,3DES)
- LDAP
- 开源日志库<log4cplus+VS2008使用>整理
- 微信内的webview中获取微信用户的基本信息(open_id)
- Android 调试工具集【转】
- 关于dedecms的0day漏洞的解决方案(个人见解)
- ssh禁止root登入
- PX4 pixhawk 和APM2.X 的USB驱动都是不能够在 windows 7 、windows 8的ghost系统下自动安装(已解决)
- Oracle to_char格式化函数
- iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)
- android 图片处理总结
- PB中调取数据库填充控件DDLB代码