導致簽名錯誤的原因:
微信文檔給出以下原因。
invalid signature簽名錯誤:建議按如下順序檢查: 確認簽名算法正確,可用
- http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=jsapisign 頁面工具進行校驗。
- 確認config中nonceStr(js中駝峯標準大寫S), timestamp與用以簽名中的對應noncestr,timestamp一致。
- 確認url是頁面完整的url(請在當前頁面alert(location.href.split(’#’)[0])確認),包括 http(s) : // 部分,以及 ? 後面的GET參數部分,但不包括 # hash後面的部分。
- 確認config中的appid與用來獲取jsapi_ticket的corpid一致。
- 確保一定緩存access_token和jsapi_ticket。
- 確保你獲取用來簽名的url是動態獲取的,動態頁面可參見實例代碼中php的實現方式。如果是html的靜態頁面在前端通過ajax將url傳到後臺簽名,前端需要用js獲取當前頁面除去’#‘hash部分的鏈接(可用location.href.split(’#’)[0]獲取,而且需要encodeURIComponent),因爲頁面一旦分享,微信客戶端會在你的鏈接末尾加入其它參數,如果不是動態獲取當前鏈接,將導致分享後的頁面簽名失敗。
自己程序檢測:
1-5 檢測都沒有問題。漫長的思考後,發現問題出在了URL。
要進行前端URL編碼,不要用後端自動獲取的。前端把編好碼的URL 直接傳給後端用。
客戶端調用示例:
<script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
axios.get('getsign?url=' + encodeURI(location.href)) //就是這樣編碼,url傳給後端即可
.then(function (response) {
// handle success
console.log(response);
wx.config({
debug: true,
appId: response.data.appId,
timestamp: response.data.timestamp,
nonceStr: response.data.nonceStr,
signature: response.data.signature,
jsApiList: [] // Required, required JA interface list, all JS interface list, see Appendix 2
});
})
</script>
後端代碼:
可以直接下載微信給出的SDK.
http://demo.open.weixin.qq.com/jssdk/sample.zip
備註:鏈接中包含php、java、nodejs以及python的示例代碼供第三方參考,第三方切記要對獲取的accesstoken以及jsapi_ticket進行緩存以確保不會觸發頻率限制。
參考微信文檔:
END.
佛祖保佑
_ooOoo_
o8888888o
88" . "88
(| -_- |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . __
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
\ \ `-. \_ __\ /__ _/ .-` / /
美女與野獸
_____ &&&&_) )
\/,---< &&&&&&\ \
( )c~c~~@~@ )- - &&\ \
C >/ \< |&/
\_O/ - 哇塞 _`*-'_/ /
,- >o<-. / ____ _/
/ \/ \ / /\ _)_)
/ /| | |\ \ / / ) |
\ \| | |/ / \ \ / |
\_\ | |_/ \ \_ |
/_/`___|_\ /_/\____|
| | | \ \|
| | | `. )
| | | / /
|__|_|_ /_/|
(____)_) |\_\_
可愛維尼小熊
┴┬┴┬/ ̄\_/ ̄\
┬┴┬┴▏ ▏▔▔▔▔\
┴┬┴/\ / ﹨
┬┴∕ / )
┴┬▏ ● ▏
┬┴▏ ▔█
┴◢██◣ \___/
┬█████◣ /
┴█████████████◣
◢██████████████▆▄
█◤◢██◣◥█████████◤\
◥◢████ ████████◤ \
┴█████ ██████◤ ﹨
┬│ │█████◤ ▏
┴│ │ ▏
┬ ∕ ∕ /▔▔▔\ ∕
┴/___/﹨ ∕ ﹨ /\
┬┴┬┴┬┴\ \ ﹨/ ﹨
┴┬┴┬┴┬┴ \___\ ﹨/▔\﹨ ▔\
▲△▲▲╓╥╥╥╥╥╥╥╥\ ∕ /▔﹨/▔﹨
**╠╬╬╬╬╬╬╬╬*﹨ / //