How to Use JS-SDK, JS-SDK use
One sentence of the JS library was used two days ago -- wx. closeWindow (); but the whole call process was a bit tearful ....
Although it is clear on the developer platform, it is not OK to use it!
1. Bind a domain name
Log on to the public platform:
In the public account settings:
Find the function settings and enter the JS interface Security Domain Name:
Ii. Introduce JS
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script>
3,
Inject the permission verification configuration through the config Interface
I feel that the call here is quite tedious, and I hope to make a secure and convenient interface for calling.
// Wx. config ({// debug: false, // enable the debug mode. The returned values of all called APIs are displayed on the alert client. To view the input parameters, open them on the pc, the parameter information is output through log and printed only on the pc end. // AppId: '<% = appId %>', // required, unique public ID // timestamp: <% = timestamp %>, // required, signature generation timestamp // nonceStr: '<% = nonceStr %>', // required. The random string of the signature is generated // signature: '<% = Signature %>', // required, Signature. For details, see Appendix 1 // jsApiList: ['closewindow'] // required. List of JS interfaces to be used, for a list of all JS interfaces, see Appendix 2 //});
The wx object provides many method calls, And the return values of these methods can be set in config to facilitate observation of error messages during debugging.
Appid, timestamp, noncestr, and jsApiList are all well-filled parameters. The only trouble is signature.
Signing steps are relatively small and complex: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
/// <Summary> /// generate The signature /// </summary> /// <param name = "jsapi_ticket"> The jsapi_ticket. </param> /// <param name = "noncestr"> The noncestr. </param> /// <param name = "timestamp"> The timestamp. </param> /// <param name = "url"> The URL. </param> /// <returns> System. string. </returns> // <remarks> Editor: v-liuhch CreateTime: 2015/6/1 18:20:43 </remarks> private string GetSignature () {WxAccount wa = WxAccountAdapt Er. instance. loadByCode (PubAccCode); // obtain the current public account WebClientBase wcb = new WebClientBase (); // obtain the Access_token information string strAccess_token = wcb. requestMyWebClient (EnumSubmitMethod. get, "", "https://api.weixin.qq.com/cgi-bin/token? Grant_type = client_credential & appid = "+ wa. appID + "& secret =" + wa. appSecret); access_tokenEntity at = WxAPITools. JSONUtil <access_tokenEntity>. fromJsonToObject <access_tokenEntity> (strAccess_token); string strJson = wcb. requestMyWebClient (EnumSubmitMethod. get, "", "https://api.weixin.qq.com/cgi-bin/ticket/getticket? Access_token = "+. access_token + "& type = jsapi"); jsapi_ticket jt = WxAPITools. JSONUtil <jsapi_ticket>. fromJsonToObject <jsapi_ticket> (strJson); // convert to object string url = HttpContext. current. request. url. toString (); url = url. indexOf ("#")> = 0? Url. substring (0, url. indexOf ("#"): url; string tmpStr = "jsapi_ticket =" + jt. ticket + "& noncestr =" + nonceStr + "× tamp =" + timestamp + "& url =" + url; return FormsAuthentication. hashPasswordForStoringInConfigFile (tmpStr, "SHA1 ");} /// <summary> /// jsapi_ticket is a temporary ticket used to call the JS interface. /// </summary> /// <remarks> Editor: v-liuhch CreateTime: 18:25:27 </remarks> public class jsapi_ticket {public string errcode {get; set;} public string errmsg {get; set;} public string ticket {get; set ;} public string expires_in {get; set ;}/// <summary> // Class access_tokenEntity /// </summary> /// <remarks> Editor: v-liuhch CreateTime: 21:08:07 </remarks> public class access_tokenEntity {public string access_token {get; set;} public string expires_in {get; set ;}}
Note which part of the URL address is obtained. get is used to obtain data. All parameters to be signed are sorted in ascending order based on the ASCII code of the field name;
If debug is enabled, a config: OK is returned.
When you confirm the interface signature, you can use the provided web page debugging tool: http://mp.weixin.qq.com/debug/cgi-bin/sandbox? T = jsapisign