WeChat JS-SDK-Use Permission Signature Algorithm

WeChat JS-SDK-Use Permission Signature Algorithm

jsapi_ticket

Before generating a signature, you must first understand jsapi_ticket. jsapi_ticket is a temporary ticket used by the official account to call the WeChat JS interface. Under normal circumstances, the validity period of jsapi_ticket is 7200 seconds and is obtained through access_token. Since the number of API calls to obtain jsapi_ticket is very limited, frequent refresh of jsapi_ticket will lead to API call restrictions and affect your own business. Developers must cache jsapi_ticket globally in their own services .

Refer to the following document to obtain access_token ( valid for 7200 seconds, developers must cache access_token globally in their own services ): ../15/54ce45d8d30b6bf6758f68d2e95bc627.html

Use the access_token obtained in the first step to obtain a jsapi_ticket using the http GET method ( valid for 7200 seconds, developers must cache the jsapi_ticket globally in their own services ): https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi

The following JSON is returned successfully:

{
"errcode":0,
"errmsg":"ok",
"ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA",
"expires_in":7200
}

After obtaining the jsapi_ticket, you can generate a signature for JS-SDK permission verification.

Signature Algorithm

The signature generation rules are as follows: The fields involved in the signature include noncestr (random string), valid jsapi_ticket, timestamp (timestamp), url (URL of the current webpage, excluding # and its following part ). After sorting all the parameters to be signed from small to large according to the ASCII code of the field name (lexicographic order), they are concatenated into string string1 using the format of URL key-value pairs (i.e. key1=value1&key2=value2…). It should be noted that all parameter names are lowercase characters. Encrypt string1 with sha1, and both the field name and field value use the original value without URL escaping.


That is, signature = sha1(string1). Example:

noncestr=Wm3WZYTPz0wzccnW

jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg

timestamp=1414587457

url=http://mp.weixin.qq.com?params=value


Step 1. Sort all parameters to be signed by the ASCII code of the field name from small to large (lexicographical order), and concatenate them into string 1 using the URL key-value pair format (i.e. key1=value1&key2=value2…):

jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOc HKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value


Step 2. Sign string1 with sha1 to get the signature:

0f9de62fce790f9a083d5c99e95740ceb90c27ed

Precautions

The noncestr and timestamp used for the signature must be the same as the noncestr and timestamp in wx.config.

The URL used for signature must be the complete URL of the page that calls the JS interface.

For security reasons, developers must implement signing logic on the server side .

<<:  Unity Awards 2015 is about to start, and good games are coming soon!

>>:  WeChat JS-SDK interface list and problem explanation

Recommend

5 Steps to Develop Your Brand on Xiaohongshu

Among the projects I have served over the years, ...

Android application marketing strategy and plan

I often visit some small restaurants with good se...

Weibo Fans Advertising and Charging Standards!

1. Introduction to Fantong What is Fantong? To pu...

Aston Martin DBX to be unveiled in 2019 Are you sure it's an SUV?

Nowadays, major luxury sports car manufacturers h...

Father’s Day copy is here, it’s from the heart, not the body! !

Father's Day is coming. They are good at many...

How did WeChat product managers and architects handle 1 billion red envelopes?

WeChat has such a huge amount of traffic, especia...

Shangsi Festival丨Come on~Come to the Dong Village to sing and party!

Today is the third day of the third lunar month T...

ocpc account and landing page optimization!

Today let’s talk about ocpc account optimization,...

3 formulas for May Day marketing activities

Holidays are often a great opportunity for busine...

Will robot sex become a reality within 10 years?

Robots will gradually move from science fiction, ...