参照博客[027] 微信公众帐号开发教程第3篇-开发模式启用及接口配置
根据微信开发者文档,启用公共平台开发者模式并进行接口配置流程如下:
加密/校验流程如下: 1. 将token、timestamp、nonce三个参数进行字典序排序 2. 将三个参数字符串拼接成一个字符串进行sha1加密 3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
#Ruby实现如下
require 'digest/sha1’
class WechatSupportsController < ApplicationController
@@token=“api_wechat"
def auth_wechat
if check_signature?(params[:signature],params[:timestamp],params[:nonce])
return render text: params[:echostr]
end
end
private
def check_signature?(signature,timestamp,nonce)
Digest::SHA1.hexdigest([timestamp,nonce,@@token].sort.join) == signature
end
end
举个栗子,我们将此处url服务器地址设置为http://abc.com/test/wechat
路由配置:get '/test/wechat',to: 'wechat_supports#auth_wechat'完成之后将代码传上服务器,在微信公共平台进行配置完成后“启用”即可。
URL(服务器地址):http://abc.com/test/wechat
Token(令牌):api_wechat(同代码中变量值@@token)