助乐 發表於 2020-7-29 23:51:00

Flask+微信公众号开发(接入指南)

<h1><span style="color: rgba(255, 102, 0, 1)">目录</span></h1>
<div><span style="color: rgba(255, 102, 0, 1)">一、注册公众号</span></div>
<div><span style="color: rgba(255, 102, 0, 1)">二、启用开发者</span></div>
<div><span style="color: rgba(255, 102, 0, 1)">三、配置服务器配置</span></div>
<div><span style="color: rgba(255, 102, 0, 1)">四、开发自己的需求</span></div>
<div><span style="color: rgba(255, 102, 0, 1)">五、写在最后</span> </div>
<h1><span style="color: rgba(255, 102, 0, 1)">一、注册公众号</span></h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 具体的注册过程,根据官方文档一步一步来即可。这里需注意的是订阅号还是服务号;有些比较好的开发接口订阅号是没有的,但是注册服务号需要企业认证之类的,比较复杂。</p>
<p>&nbsp;</p>
<h1><span style="color: rgba(255, 102, 0, 1)">二、启用开发者</span></h1>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在公众号后台打开启用即可。</p>
<p>&nbsp;</p>
<h1><span style="color: rgba(255, 102, 0, 1)">三、配置服务器配置</span></h1>
<p><strong><span style="color: rgba(255, 0, 0, 1)">注:</span></strong>整个过程是动态的,是得先把后端的web和代码设置好,再来配置服务器配置,服务器配置的成功与否是会正儿八经的请求你填的这个URL去做校验的。刚开始的时候不太了解,以为只是填一下信息就行,一直token错误,最好的办法是边开发,边调试。</p>
<p>&nbsp;</p>
<p><img src="https://img2020.cnblogs.com/blog/746846/202007/746846-20200729234105169-1737059240.png" alt=""></p>
<div><strong><span style="color: rgba(0, 128, 0, 1)">1、配置服务器的四个参数</span></strong></div>
<div>
<p><img src="https://img2020.cnblogs.com/blog/746846/202007/746846-20200729234211604-898760721.png" alt=""></p>
</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>1)URL:请求到web服务器的地址</div>
<div>2)Token:任意填写 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</div>
<div>3)EncodingAESKey:随机生成&nbsp;</div>
<div>4)消息加解密方式:明文方式 &nbsp;&nbsp;</div>
<div>&nbsp;</div>
<div><strong><span style="color: rgba(0, 128, 0, 1)">2、Web请求过去的四个参数</span></strong></div>
<div>&nbsp;</div>
<div><strong><span style="color: rgba(255, 0, 0, 1)">/?signature=xxx&amp;echostr=xxx&amp;timestamp=xxx&amp;nonce=xxx</span></strong></div>
<div>&nbsp;</div>
<div>1)signature 加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; signature 加密签名 = token(开发者)+ timestamp参数 + nonce参数</div>
<div>2)echostr 随机字符串</div>
<div>3)timestamp 时间戳</div>
<div>4)nonce 随机数</div>
<div>&nbsp;</div>
<div><strong><span style="color: rgba(0, 128, 0, 1)">3、后端服务器</span></strong></div>
<div>&nbsp;</div>
<div>后端服务器将通过检验signature对请求进行校验,校验方法如下:</div>
<div>&nbsp;</div>
<div>1)获取请求的四个参数signature、echostr、timestamp、nonce</div>
<div>2)对token(代码里指定)、timestamp参数、nonce参数进行字典排序</div>
<div>3)将三个参数字符串拼接成一个字符串并进行sha1加密</div>
<div>4)对加密后的字符串与请求获取的signature对比,如果一样,返回echostr,对接成功</div>
<div>&nbsp;</div>
<div><strong><span style="color: rgba(0, 128, 0, 1)">4、代码实现</span></strong></div>
<div>&nbsp;</div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">from</span> flask <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> Flask
</span><span style="color: rgba(0, 0, 255, 1)">from</span> flask <span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> request
</span><span style="color: rgba(0, 0, 255, 1)">import</span><span style="color: rgba(0, 0, 0, 1)"> hashlib

app </span>= Flask(<span style="color: rgba(128, 0, 128, 1)">__name__</span><span style="color: rgba(0, 0, 0, 1)">)

@app.route(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/wechat</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">def</span><span style="color: rgba(0, 0, 0, 1)"> wechat():

    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 1、 获取携带的 signature、timestamp、nonce、echostr</span>
    signature = request.args.get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">signature</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    timestamp </span>= request.args.get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">timestamp</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    nonce </span>= request.args.get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">nonce</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    echostr </span>= request.args.get(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">echostr</span><span style="color: rgba(128, 0, 0, 1)">"</span>, <span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">)
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(signature, timestamp, nonce, echostr)

    token</span>=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">xxxxxxxxx</span><span style="color: rgba(128, 0, 0, 1)">"</span>

    <span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 2、 进行字典排序</span>
    data =<span style="color: rgba(0, 0, 0, 1)">
    data.sort()

    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 3、三个参数拼接成一个字符串并进行sha1加密</span>
    temp = <span style="color: rgba(128, 0, 0, 1)">''</span><span style="color: rgba(0, 0, 0, 1)">.join(data)
    sha1 </span>= hashlib.sha1(temp.encode(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">))
    hashcode </span>=<span style="color: rgba(0, 0, 0, 1)"> sha1.hexdigest()
    </span><span style="color: rgba(0, 0, 255, 1)">print</span><span style="color: rgba(0, 0, 0, 1)">(hashcode)

    </span><span style="color: rgba(0, 128, 0, 1)">#</span><span style="color: rgba(0, 128, 0, 1)"> 4、对比获取到的signature与根据上面token生成的hashcode,如果一致,则返回echostr,对接成功</span>
    <span style="color: rgba(0, 0, 255, 1)">if</span> hashcode ==<span style="color: rgba(0, 0, 0, 1)"> signature:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> echostr
    </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">error</span><span style="color: rgba(128, 0, 0, 1)">"</span>


<span style="color: rgba(0, 0, 255, 1)">if</span> <span style="color: rgba(128, 0, 128, 1)">__name__</span> == <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">__main__</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">:
    app.run(host</span>=<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">0.0.0.0</span><span style="color: rgba(128, 0, 0, 1)">'</span>, port=5000, debug=True)</pre>
</div>
<p>&nbsp;</p>
<h1><span style="color: rgba(255, 102, 0, 1)">五、写在最后</span></h1>
<div>在平凡中坚持前行,总有一天会遇见不一样的自己。</div>
<div>&nbsp;</div>
<div>写博客记录、思考、总结,趟过的坑不趟第二遍。</div>
<div>&nbsp;</div>
<p>所有的文章,皆同步在公众号“运维汪”,可关注;也可加入“不扯淡,专注于技术”的QQ群:753512236 </p>
<p><img src="https://img2020.cnblogs.com/blog/746846/202007/746846-20200729234852499-835428600.png" alt="" loading="lazy"></p>
<p>&nbsp; </p>

</div>
<div id="MySignature" role="contentinfo">
    <div>作者:李先生</div>
<div>出处:https://www.cnblogs.com/lemon-le/
</div>

<p>-------------------------------------------</p>
<p>个性签名:在平凡中坚持前行,总有一天会遇见不一样的自己!</p>
<p>如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个<span>“推荐”</span>哦,博主在此感谢!</p>
<p></p>
<p>万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!</p>
<img src="https://images.cnblogs.com/cnblogs_com/lemon-le/871591/o_w2.png" alt="微信公众号">
<img src="https://www.cnblogs.com/images/cnblogs_com/lemon-le/871591/o_wechat.png" alt="微信打赏">
<img src="https://www.cnblogs.com/images/cnblogs_com/lemon-le/871591/o_zhifubao.png" alt="微信打赏">
<p>&nbsp;&nbsp;&nbsp;微信公众号&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;微信打赏&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;支付宝打赏</p><br><br>
来源:https://www.cnblogs.com/lemon-le/p/13401423.html
頁: [1]
查看完整版本: Flask+微信公众号开发(接入指南)