WeChat Development token verification failure resolution method

Source: Internet
Author: User
Tags cdata sha1 sha1 encryption
This article describes the development of a token verification failed to solve the problem, the development of interest or have encountered token verification failure can not be resolved, may see this article Oh! Talk less, let's get to the chase!

Small program configuration message push generally will appear token verification failure problem, This error is because your interface page has not feedback the correct information to the interface, netizens also gave some solutions, but some can be configured to succeed, some are not. The following gives the user 2 kinds of relatively easy to configure the successful PHP interface verification code.

code example one (my validation can succeed):

<?php  //1. Timestamp, nonce, token sorted by dictionary  $timestamp = $_get[' timestamp ');  $nonce = $_get[' nonce '];  $token = "Your custom token value";  $signature = $_get[' signature ');  $array = Array ($timestamp, $nonce, $token);  Sort ($array);    2. After the sorting of the three parameters after stitching with SHA1 encryption  $TMPSTR = Implode (", $array);  $TMPSTR = SHA1 ($TMPSTR);    3. Compares the encrypted string to signature to determine whether the request is from the  if ($tmpstr = = $signature)  {      echo $_get[' echostr ');      Exit;  }

code example two:

<?php/** * WeChat PHP Test *///define Your token define ("token", "Custom token");  $WECHATOBJ = new Wechatcallbackapitest ();    $WECHATOBJ->valid ();            Class Wechatcallbackapitest {public function valid () {$echoStr = $_get["Echostr"];              Valid signature, option if ($this->checksignature ()) {echo $echoStr;          Exit          }} Public Function responsemsg () {//get-post data, May is due to the different environments            $POSTSTR = $GLOBALS ["Http_raw_post_data"]; Extract post Data if (!empty ($POSTSTR)) {$POSTOBJ = simplexml_load_string ($p                  Oststr, ' simplexmlelement ', libxml_nocdata);                  $fromUsername = $POSTOBJ->fromusername;                  $toUsername = $POSTOBJ->tousername;                  $keyword = Trim ($postObj->content);                  $time = time ();  $TEXTTPL = "<xml>                            <tousername><! [cdata[%s]]></tousername> <fromusername><!                              [cdata[%s]]></fromusername> <CreateTime>%s</CreateTime> <msgtype><! [cdata[%s]]></msgtype> <content><!                              [cdata[%s]]></content> <FuncFlag>0</FuncFlag>                               </xml> ";                      if (!empty ($keyword)) {$msgType = "text";                      $CONTENTSTR = "Welcome to WeChat world!";                      $RESULTSTR = sprintf ($TEXTTPL, $fromUsername, $toUsername, $time, $msgType, $CONTENTSTR);                  Echo $resultStr;                  }else{echo "Input something ...";              }}else {echo "";          Exit}} Private Function Checksignature () {$signature = $_get["signature"];          $timestamp = $_get["timestamp"];                                $nonce = $_get["nonce"];          $token = token;          $TMPARR = Array ($token, $timestamp, $nonce);          Sort ($TMPARR);          $TMPSTR = implode ($TMPARR);                    $TMPSTR = SHA1 ($TMPSTR);          if ($tmpStr = = $signature) {return true;          }else{return false; }}}?>

The above two instances of the code select a direct upload to your server, in the message configuration URL (server address to write the server address of PHP file), fill in the corresponding custom token (token), the message encryption key randomly generated, encryption mode I filled in the compatibility mode, data format with personal preferences ( I filled in the JSON). Then click Submit directly. If a description appears, the validation is passed:



We have met the token verification error can see Oh! help you solve the problem of tokens faster!

Related recommendations:

Token of the App interface

How to set up a applet URL and token

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.