神仙眷侣 發表於 2020-8-19 11:43:00

微信公众号开发【2、服务器搭建】

<p>一、注册账号</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;百度微信公众平台,点击进入。</p>
<p>二、公众平台测试账号</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;点击进入平台后居然是小程序,我也很费解。以前是找到开发-&gt;开发者工具-&gt;公众平台测试账号,现在毛都没有了。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819112932421-2125581612.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; 不过可以点击这里调转。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;点击这里进入。</p>
<p>三、进入公众平台。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;这里好像要微信扫码登录。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819113205694-1728700822.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 上面的接口配置信息是给接口服务器端使用,下面的JS接口安全域名是给前端调用。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 这里的坑还是多,而且官方文档含糊其辞,这点差评。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 首先就是为何要有这个域名。说的直白一点,就是你要想和微信第三方接口通讯,必须要有一个能被外网访问的接口暴露出去供第三方调用。所以你需要有一个服务器和域名。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 这里需要注意:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1、服务器随便搞。我的是阿里云的云服务器。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 2、域名,这里必须要用域名,不然会配置失败。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 3、必须是http的80端口或者https的443默认端口。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 4、URL的方法是GET请求,方法名随意。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 5、必须把服务器ip添加到白名单。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 添加到白名单在微信公众平台添加。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819113847807-171358927.png"></p>
<p>&nbsp;</p>
<p>&nbsp;四、现在来测试一下。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819114117685-1844549116.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; js同样的这样就可以了。</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819114142691-739399447.png"></p>
<p>&nbsp;五、微信在线测试接口</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;https://mp.weixin.qq.com/debug</p>
<p>六、代码</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <strong><span style="color: rgba(255, 0, 0, 1)">服务器后台API接口采用.net core 3.0版本以上。</span></strong></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 6.1、接口方法</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<div class="cnblogs_code"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_ba26375d-3735-4f52-b21a-3471cd16f52c" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_ba26375d-3735-4f52-b21a-3471cd16f52c" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>         <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 检测接口
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>         <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 作用:在注册微信公众号的测试号管理界面,提供接口配置信息。接收微信接入验证请求,并给与响应。
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="signature"&gt;</span><span style="color: rgba(0, 128, 0, 1)">微信加密签名</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">时间戳</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="nonce"&gt;</span><span style="color: rgba(0, 128, 0, 1)">随机数</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="echostr"&gt;</span><span style="color: rgba(0, 128, 0, 1)">随机字符串</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>         
</span><span style="color: rgba(0, 128, 128, 1)">11</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">string</span> CheckWeChat(<span style="color: rgba(0, 0, 255, 1)">string</span> signature, <span style="color: rgba(0, 0, 255, 1)">string</span> timestamp, <span style="color: rgba(0, 0, 255, 1)">string</span> nonce, <span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> echostr)
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)">13</span>             log.Debug(<span style="color: rgba(128, 0, 0, 1)">"</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, 128, 128, 1)">14</span>             log.Debug($<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">signature:{signature},timestamp:{timestamp},nonce:{nonce},echostr:{echostr}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">15</span>            
<span style="color: rgba(0, 128, 128, 1)">16</span>             <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">string</span>.IsNullOrWhiteSpace(signature) &amp;&amp; !<span style="color: rgba(0, 0, 255, 1)">string</span>.IsNullOrWhiteSpace(timestamp) &amp;&amp; !<span style="color: rgba(0, 0, 255, 1)">string</span>.IsNullOrWhiteSpace(nonce) &amp;&amp; !<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)">.IsNullOrWhiteSpace(echostr))
</span><span style="color: rgba(0, 128, 128, 1)">17</span> <span style="color: rgba(0, 0, 0, 1)">            {
</span><span style="color: rgba(0, 128, 128, 1)">18</span>               log.Debug(<span style="color: rgba(128, 0, 0, 1)">"</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, 128, 128, 1)">19</span>
<span style="color: rgba(0, 128, 128, 1)">20</span>               <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">读取配置Token,需要和你填写的一样</span>
<span style="color: rgba(0, 128, 128, 1)">21</span>               <span style="color: rgba(0, 0, 255, 1)">var</span> token = JsonFileHelper.Read(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">WeChat_TestToken</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 128, 1)">22</span>
<span style="color: rgba(0, 128, 128, 1)">23</span>               <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">string</span>.IsNullOrWhiteSpace(token) &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> WeChatHelper.VerificationSignature(echostr, nonce, signature, timestamp, token))
</span><span style="color: rgba(0, 128, 128, 1)">24</span> <span style="color: rgba(0, 0, 0, 1)">                {
</span><span style="color: rgba(0, 128, 128, 1)">25</span>                     log.Debug(<span style="color: rgba(128, 0, 0, 1)">"</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, 128, 128, 1)">26</span>                     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">如果调用成功,返回随机字符串</span>
<span style="color: rgba(0, 128, 128, 1)">27</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, 128, 128, 1)">28</span> <span style="color: rgba(0, 0, 0, 1)">                }
</span><span style="color: rgba(0, 128, 128, 1)">29</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">30</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)">false</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">31</span>         }</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>&nbsp; &nbsp; &nbsp; &nbsp;6.2、帮助类</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;</p>
<div class="cnblogs_code"><img id="code_img_closed_c2ba2e1d-694c-4f76-ba4b-c2f8d6e31dd8" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_c2ba2e1d-694c-4f76-ba4b-c2f8d6e31dd8" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_c2ba2e1d-694c-4f76-ba4b-c2f8d6e31dd8" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">string</span> GetSha1Hash(<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> str)
</span><span style="color: rgba(0, 128, 128, 1)"> 2</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>             <span style="color: rgba(0, 0, 255, 1)">var</span> inputBytes =<span style="color: rgba(0, 0, 0, 1)"> Encoding.Default.GetBytes(str);
</span><span style="color: rgba(0, 128, 128, 1)"> 4</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>             SHA1 sha = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> SHA1CryptoServiceProvider();
</span><span style="color: rgba(0, 128, 128, 1)"> 6</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>             <span style="color: rgba(0, 0, 255, 1)">var</span> result =<span style="color: rgba(0, 0, 0, 1)"> sha.ComputeHash(inputBytes);
</span><span style="color: rgba(0, 128, 128, 1)"> 8</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>             <span style="color: rgba(0, 0, 255, 1)">var</span> sBuilder = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder();
</span><span style="color: rgba(0, 128, 128, 1)">10</span>
<span style="color: rgba(0, 128, 128, 1)">11</span>             <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; result.Length; i++<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">12</span> <span style="color: rgba(0, 0, 0, 1)">            {
</span><span style="color: rgba(0, 128, 128, 1)">13</span>               sBuilder.Append(result.ToString(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">x2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 128, 1)">14</span> <span style="color: rgba(0, 0, 0, 1)">            }
</span><span style="color: rgba(0, 128, 128, 1)">15</span>
<span style="color: rgba(0, 128, 128, 1)">16</span>             <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> sBuilder.ToString();
</span><span style="color: rgba(0, 128, 128, 1)">17</span>         }</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<div class="cnblogs_code"><img id="code_img_closed_ec86dfea-4b9e-44b9-b969-b73c247a5d1e" class="code_img_closed lazyload" data-src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_ec86dfea-4b9e-44b9-b969-b73c247a5d1e" class="code_img_opened lazyload" style="display: none" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif">
<div id="cnblogs_code_open_ec86dfea-4b9e-44b9-b969-b73c247a5d1e" class="cnblogs_code_hide">
<pre><span style="color: rgba(0, 128, 128, 1)"> 1</span> <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;summary&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 2</span>         <span style="color: rgba(128, 128, 128, 1)">///</span><span style="color: rgba(0, 128, 0, 1)"> 签名验证
</span><span style="color: rgba(0, 128, 128, 1)"> 3</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;/summary&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 4</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="echostr"&gt;</span><span style="color: rgba(0, 128, 0, 1)">随机字符串</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 5</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="nonce"&gt;</span><span style="color: rgba(0, 128, 0, 1)">随机数</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 6</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="signature"&gt;</span><span style="color: rgba(0, 128, 0, 1)">微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 7</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="timestamp"&gt;</span><span style="color: rgba(0, 128, 0, 1)">时间戳</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 8</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;param name="token"&gt;</span><span style="color: rgba(0, 128, 0, 1)">用户签名</span><span style="color: rgba(128, 128, 128, 1)">&lt;/param&gt;</span>
<span style="color: rgba(0, 128, 128, 1)"> 9</span>         <span style="color: rgba(128, 128, 128, 1)">///</span> <span style="color: rgba(128, 128, 128, 1)">&lt;returns&gt;&lt;/returns&gt;</span>
<span style="color: rgba(0, 128, 128, 1)">10</span>         <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">bool</span> VerificationSignature(<span style="color: rgba(0, 0, 255, 1)">string</span> echostr, <span style="color: rgba(0, 0, 255, 1)">string</span> nonce, <span style="color: rgba(0, 0, 255, 1)">string</span> signature, <span style="color: rgba(0, 0, 255, 1)">string</span> timestamp,<span style="color: rgba(0, 0, 255, 1)">string</span><span style="color: rgba(0, 0, 0, 1)"> token)
</span><span style="color: rgba(0, 128, 128, 1)">11</span> <span style="color: rgba(0, 0, 0, 1)">      {
</span><span style="color: rgba(0, 128, 128, 1)">12</span>             <span style="color: rgba(0, 0, 255, 1)">string</span>[] StrArray =<span style="color: rgba(0, 0, 0, 1)"> { token, timestamp, nonce };
</span><span style="color: rgba(0, 128, 128, 1)">13</span> <span style="color: rgba(0, 0, 0, 1)">            Array.Sort(StrArray);
</span><span style="color: rgba(0, 128, 128, 1)">14</span>             <span style="color: rgba(0, 0, 255, 1)">var</span> ArrayString = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> StringBuilder();
</span><span style="color: rgba(0, 128, 128, 1)">15</span>             <span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">var</span> i = <span style="color: rgba(128, 0, 128, 1)">0</span>; i &lt; StrArray.Length; i++<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 128, 1)">16</span> <span style="color: rgba(0, 0, 0, 1)">                ArrayString.Append(StrArray);
</span><span style="color: rgba(0, 128, 128, 1)">17</span>             <span style="color: rgba(0, 0, 255, 1)">var</span> strHash =<span style="color: rgba(0, 0, 0, 1)"> GetSha1Hash(ArrayString.ToString());
</span><span style="color: rgba(0, 128, 128, 1)">18</span>             <span style="color: rgba(0, 0, 255, 1)">return</span> signature ==<span style="color: rgba(0, 0, 0, 1)"> strHash;
</span><span style="color: rgba(0, 128, 128, 1)">19</span>         }</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<p>&nbsp; &nbsp; &nbsp; 6.3、截图</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img alt="" loading="lazy" src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819132653300-474740571.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819132726273-2076682546.png"></p>
<p>&nbsp;</p>
<p>&nbsp;七、远程服务器代码发布</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2020.cnblogs.com/blog/934263/202008/934263-20200819132818075-177497139.png"></p>
<p>&nbsp;</p>
<p>&nbsp; &nbsp; &nbsp;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;想要源码的可以联系我。希望对大家有帮助。</p><br><br>
来源:https://www.cnblogs.com/xiaobaicai12138/p/13528392.html
頁: [1]
查看完整版本: 微信公众号开发【2、服务器搭建】