JS-SDK solution micro-channel access function sweep the signature is not valid and invalid signature appears

First, start with about JS-SDK function sweep the micro-channel access methods:

Access First Steps, slightly mention here, micro-channel to develop very detailed document which stresses can refer to the write:

I focus here talk about the implementation process:

The first is to introduce the corresponding js file on the page, one line of code will suffice:

(支持https):http://res.wx.qq.com/open/js/jweixin-1.2.0.js

Then verify permissions configured via interface injection config:

First, look at the introduction of the development of micro-channel official document:

It should be passed parameters from the server side web surface has timestamp, nonceStr and the signature and jsApiList appId are fixed, where the direct write process to write back. First, write server-side code generation timestamp, nonceStr and signature.

 When generating a timestamp, nonceStr signature and has two parameters need to acquire  one access_token, the other is jsapi_ticket.

acquisition requires access_token AppId and AppSecret, obtaining the following positions:

Get the following address , send a GET request

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

Access_token way to get here is not to say that a lot of this is generally not the case online as long as appId and appsercret correct address is correct to OK a big mistake.

Let us focus jsapi_ticket acquisition (here I can only talk about my own :) acquisition method:

In common tools CommonUtil written inside a method of obtaining jsapi_ticket:

JSAPI_TICKET it's that I get jsapi_ticket address defined:

public final static String JSAPI_TICKET = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";

method:

Then available to jsapi_ticket according to the acquired access_token call this method.

However :

Access_token to ensure a certain cache and jsapi_ticket.

access_token is a globally unique number of bills the public, the public will need to use access_token number when calling each interface. Developers need to be properly preserved.

access_token and jsapi_ticket must be in its own cache server, otherwise the on-line trigger frequency limit. Make sure to do some caching token and ticket twice to reduce server requests, not only to avoid triggering frequency limit, make sure that the services and certain global cache access_token jsapi_ticket front line, both of which are valid for 7200 seconds, or if the line trigger frequency limit , the service will no longer be available.

Please read the detailed documentation carefully.

After obtaining jsapi_ticket, you can generate a signature JS-SDK validated the authority .

The above is the signature generation rules developed micro-channel document:

Generate specific procedure code can not write their own, develop micro-channel document has demo examples provided:

 

What language is used directly take over on it! Can be achieved while writing while pondering the signature algorithm!

Specific code I will not stick out, and basically the same as the above demo written.

Controller then the code layers:

Which JsApiTicketUtil.getJsApiTicket (); I got from the cache jsapi_ticket of pairs.

JsSignUtil.sign (jsapi_ticket, url); that is, the demo way to get a signature.

The background process is substantially complete, the next step is on the page:

在config中进行配置:

由于后台中生成的签名时间随机字符串什么的都是我在前台页面上用ajax获取到的,所有我写的配置为

这个示例是我写微信扫一扫的示例。其中的jsApiList可以有很多值,微信开发文档上都用。

然后写一个简单的扫一扫按钮 写个触发事件:

不管是按部就班还是自己琢磨着写,总算是写下来了,接下来就是对自己写的进行测试了:

页面上功能按部就班的写完点击扫一扫没反应???WFT?只好把配置文件config的debug打开了。

发现每次打开页面总是会提示签名无效:

几乎整整一两天时间都再整这个bug,现在已经解决了,接下来我说说解决过程:

首先肯定是先看微信开发文档上给出的解决办法,这个也是一般非常常用的解决办法:

https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

截图:

如果出现签名无效 首先请按照官网文档上的方法一步步去排查,当然,排查过N次的同学就不用看了。

如果测试N次总是出现签名无效 ,那肯定是签名有问题。

如果用签名页面工具校验校验签名确实无误的话 ,那就是url的问题了。

微信开发文档常见错误报错上有这样一句话:

所以url要动态获取:可用location.href.split('#')[0]获取,而且需要encodeURIComponent,例如我写的:

然后后台获得还需要转换回来:

这是url的问题。一般这样写的话基本不会出什么错误了,要是还是出现签名无效,请继续往下看;

请确认你的AppId和AppSecret正确性;

确定通过AppId和AppSecret获取到的access_token全局缓存了并且有效。

确认获取到的jsapi_ticket是有效的 用到的access_token和appid是正确的。

确认获取access_token和jsapi_ticket的地址为:

 

请一定要仔细检查这个地址url,最好和官网文档上的对着一遍,看清楚是获取什么的地址栏!

最后再补充一点:

在查找错误的时候,我在百度上看到有人这样说:

上面的两个方法:是签名算法中的随机数和时间戳,首先请注意时间单位是秒 不是毫秒,其次是上面的随机数,官网文档上用的UUID获取的随机数,但是网上有人说必须是16位的,我这里写的也是16位的,如果经过上面的还报错的话

可以试试是不是这个的问题,时间紧我也没试呢!

遇到问题一定要先去仔细阅读文档,大部分答案都在文档中,都是自己粗心大意犯下的错。然后去百度,一步步排查,想不出来就想点别的总会解决的。

不说了,溜了溜了。

给个微信开发文档的路径:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432

发布了48 篇原创文章 · 获赞 4 · 访问量 3万+

Guess you like

Origin blog.csdn.net/qq_39879632/article/details/80571619