因?yàn)橹恍枰@取open id,不用去讓用戶授權(quán)一次。代碼和一些處理邏輯還參考了ruby china的另一個(gè)帖子,但忘記鏈接了
在公眾號(hào)里點(diǎn)了按鈕到check_openid的路由下,頁(yè)面空白,也沒(méi)有報(bào)錯(cuò)啥的,檢查了log里沒(méi)有我想要的openid出現(xiàn)...我把微信里的回調(diào)url改成了get_code的路由,依然日志里沒(méi)有我想要的東西..不知道到底哪一步有問(wèn)題。
參考的這個(gè)文檔:http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html#.E7.AC.AC.E4.B8.80.E6.AD.A5.EF.BC.9A.E7.94.A8.E6.88.B7.E5.90.8C.E6.84.8F.E6.8E.88.E6.9D.83.EF.BC.8C.E8.8E.B7.E5.8F.96code
-
公眾號(hào)花了300塊,是有這個(gè)接口權(quán)限的,安全域名我也添加了服務(wù)器的域名。
不知道若沒(méi)有花300塊是否就沒(méi)有這個(gè)接口的使用權(quán)限
沒(méi)有調(diào)試成功的原因是:在wx文檔里說(shuō)安全域名不能有前綴http或https,于是我誤以為回調(diào)redirect_uri也不能要,這個(gè)“我以為”就是調(diào)試不成功的原因,加上http或https就可以了?;卣{(diào)路由就是get_code或check_openid。
class Api::WxpaymentsController < ApplicationController
# get_code function exists for testing, could be dumped
respond_to :json
def get_code
code = params[:code]
state = params[:state]
Rails.logger.info("=====")
Rails.logger.info(code)
Rails.logger.info ("*****")
Rails.logger.info (state)
api_render_result(t, E_SUCCESS)
return
end
def check_openid
if session[:openid].blank?
code = params[:code]
if code.nil?
# with state params
# redirect_to "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{WXPAY_APPID}&redirect_uri=#{REDIRECT_URL}&response_type=code&scope=snsapi_base&state=#{REDIRECT_URL}#wechat_redirect"
# without state
redirect_to "https://open.weixin.qq.com/connect/oauth2/authorize?appid=#{WXPAY_APPID}&redirect_uri=#{REDIRECT_URL2}&response_type=code&scope=snsapi_base#wechat_redirect"
end
begin
url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=#{WXPAY_APPID}&secret=#{WXPAY_SECRET}&code=#{code}&grant_type=authorization_code"
session[:openid] = JSON.parse(URI.parse(url).read)["openid"]
rescue Exception => e
#
end
end
if session[:openid]
Rails.logger.info("Get the openid ~!--->#{session[:openid]}")
Rails.logger.info("Get the code ~!--->#{code}")
end
# api_render_result(t, E_SUCCESS)
end
end