微信JS-SDK中config接口注入权限验证配置
2017-08-10 22:45
881 查看
微信开发平台文档:
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名,见附录1
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
appid参数的获取:
登录微信公众号:“开发”--》“基本配置”--》“公众号开发信息”,如图,就可以获取appid参数了:
timestamp参数的获取:
nonceStr参数的获取:随意一个字符串即可;
signature参数的获取:
首先需要需要获得全局的access_token:
得到全局的access_token之后,调用接口:
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
返回json:
{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in":7200
}
通过json我们可以得到jsapi_ticket:
最终通过得到的三个参数获得signature参数:
wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '', // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: '', // 必填,生成签名的随机串
signature: '',// 必填,签名,见附录1
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
appid参数的获取:
登录微信公众号:“开发”--》“基本配置”--》“公众号开发信息”,如图,就可以获取appid参数了:
timestamp参数的获取:
/// <summary> /// 将c# DateTime时间格式转换为Unix时间戳格式 /// </summary> /// <param name="time">时间</param> /// <returns>long</returns> private long ConvertDateTimeInt(System.DateTime time) { System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)); long t bed3 = (time.Ticks - startTime.Ticks) / 10000; //除10000调整为13位 return t; }
nonceStr参数的获取:随意一个字符串即可;
signature参数的获取:
首先需要需要获得全局的access_token:
private object GetGongZhongHaoTokenParam(string AppId, string AppSecret) { System.Web.Caching.Cache objCache = HttpContext.Cache; object myToken = objCache["access_token"]; if (myToken == null) { CookieContainer myCookieContainer = new CookieContainer(); string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", AppId, AppSecret); JObject ja = JObject.Parse(GetWebContent(url, ref myCookieContainer)); string token = ja["access_token"].ToString(); int expires = int.Parse(ja["expires_in"].ToString()); objCache.Insert("access_token", token, null, DateTime.Now.AddSeconds(expires - 1000), System.Web.Caching.Cache.NoSlidingExpiration); myToken = objCache["access_token"]; } return myToken; } private string GetWebContent(string url, ref CookieContainer myCookieContainer) { string curNewUrl = string.Empty; string html = string.Empty; HttpWebRequest req = null; //如果是发送HTTPS请求 if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); req = WebRequest.Create(url) as HttpWebRequest; req.ProtocolVersion = HttpVersion.Version10; } req = (HttpWebRequest)WebRequest.Create(url); req.Method = "Post"; req.AllowAutoRedirect = false; req.KeepAlive = true; req.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)"; req.Accept = "text/html,application/xhtml+xml,*/*"; req.CookieContainer = myCookieContainer; req.ContentType = "application/x-www-form-urlencoded"; using (HttpWebResponse wr = (HttpWebResponse)req.GetResponse()) { wr.Cookies = req.CookieContainer.GetCookies(req.RequestUri); curNewUrl = wr.ResponseUri.OriginalString; myCookieContainer.Add(wr.Cookies); List<Cookie> cookies = GetAllCookies(myCookieContainer); myCookieContainer = new CookieContainer(); for (int i = 0; i < cookies.Count; i++) { myCookieContainer.Add(cookies[i]); } Stream responseStream = wr.GetResponseStream(); StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8); html = streamReader.ReadToEnd(); } return html; }
得到全局的access_token之后,调用接口:
https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
返回json:
{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in":7200
}
通过json我们可以得到jsapi_ticket:
object myTicket = null; //string openid = string.Empty;//openid if (myTicket == null) { CookieContainer myCookieContainer = new CookieContainer(); string url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", myToken.ToString()); JObject ja = JObject.Parse(GetWebContent(url, ref myCookieContainer)); string token = ja["ticket"].ToString(); //openid = ja["openid"].ToString(); //获取openid objCache.Insert("ticket", token, null, DateTime.Now.AddSeconds(3600), System.Web.Caching.Cache.NoSlidingExpiration); myTicket = objCache["ticket"]; }
最终通过得到的三个参数获得signature参数:
string stream = string.Format("jsapi_ticket={0}&noncestr={1}×tamp={2}&url={3}", (string)myTicket, noncestr(随机字符串), timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分)); //url为:Request.Url.AbsoluteUri ViewBag.MySignature = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(stream, "SHA1");
相关文章推荐
- C#微信开发-微信JS-SDK(1)之通过config接口注入权限验证配置
- 微信开发之——JSSDK,通过config接口注入权限验证配置
- 企业微信JS-SDK开发(一)------通过config接口注入权限验证配置
- C#微信网页开发---JSSDK使用 通过config接口注入权限验证配置
- ASP.NET WEBAPI实现微信JS-SDK接口注入权限验证配置
- 微信开发(2):微信js sdk分享朋友圈,朋友,获取config接口注入权限验证(java)
- java+jsp微信开发入门教程 jssdk,通过config接口注入权限验证配置,微信开发教程
- [微信JSSDK] 解决SDK注入权限验证 安卓正常,IOS出现config fail
- 微信JS-SDK config OK,但分享配置失败(分享接口回调函数失效)
- Node.js 配置微信 JS-SDK 权限验证服务
- PHP实现微信JsApi接口--权限验证配置参数的获取
- 微信调用照相拍照等 js 接口的权限配置 和 照片上传和下载实现
- 微信JS SDK配置授权,实现分享接口
- 微信JS SDK配置授权,实现分享接口
- PHP+TP框架生成使用微信JS-SDK所需的配置信息,并验证
- 关于微信中JS-SDK的接口验证过程详细说明
- 配置并学习微信JS-SDK(2)—图片接口
- 配置并学习微信JS-SDK(2)—扫一扫接口
- 解析微信JS-SDK配置授权,实现分享接口
- 解析微信JS-SDK配置授权,实现分享接口