zoukankan      html  css  js  c++  java
  • 微信测试帐号如何设置URL和Token,以及相关验证的原理

    首先说明,本帮助文档是利用javaweb的Servlet来进行“接口配置信息配置信息”认证的。

    在学习微信公众号开发的时候,读到填写服务器配置的帮助部分,总是不能理解为啥按照他的步骤做总是设置失败(吐槽:这个帮助写的太简略,原理讲了一般,对不了解PHP的人简直就是歧视)。

    设置失败的一个重要原因:url的请求相应没有返回echostr这个参数。

    如果读到这里你还不能理解,就请看以下的截图和源代码:

    1、首先我搭建了一个将局域网内我的电脑发布到intetnet上的环境,利用ngrok,如果想了解如何搭建,请查看我的另一篇博客:ngrok使用命令帮助

     1 @WebServlet("/portal")
     2 public class ServerPortal extends HttpServlet {
     3     private static final long serialVersionUID = 1L;
     4     private static final String token = "janken";
     5 
     6     /**
     7      * @see HttpServlet#HttpServlet()
     8      */
     9     public ServerPortal() {
    10         super();
    11     }
    12 
    13     /**
    14      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    15      */
    16     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    17         String signature = request.getParameter("signature");
    18         String timestamp = request.getParameter("timestamp");
    19         String nonce = request.getParameter("nonce");
    20         String echostr = request.getParameter("echostr");
    21         System.out.println("signature:" + signature);
    22         System.out.println("timestamp:" + timestamp);
    23         System.out.println("nonce:" + nonce);
    24         System.out.println("echostr:" + echostr);
    25         PrintWriter pw = response.getWriter();
    26         pw.append(echostr);
    27         pw.flush();
    28     }
    29 
    30     /**
    31      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    32      */
    33     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    34         doGet(request, response);
    35     }
    36 
    37 }

    4、根据ngrok提供的url和我们创建的servlet填写微信的url和token

    5、点击提交,这是微信会主动访问我们写的servlet,获得我们返回的参数中的echostr的内容即可认证通过。

    认证过程原理分析:

  • 相关阅读:
    Python 写文件
    Python 读文件
    Python 打开文件(File Open)
    Python 异常处理(Try...Except)
    Python PIP包管理器
    Python 正则表达式(RegEx)
    Python JSON
    Python 模块
    Python 迭代器(Iterator)
    Python 继承
  • 原文地址:https://www.cnblogs.com/janken/p/5593737.html
Copyright © 2011-2022 走看看