在Rails中,使用 Omniauth 来做微信认证 (这个是最最完整版)

访问量: 1140

1. 使用两个Gem: 

# Gemfile: 
 gem 'omniauth', '1.3.1'
 gem 'omniauth-wechat-oauth2', '0.1.0'

2. 增加对应的配置文件: 

# config/application.yml 
   wx_pay:
     appid: 'wxae2ff723????985a'
     key: 'tAjcyEypGZRej????onSpUiHtXxPUysYb'

3. 增加 config/initializers/omni_auth.rb

   # -*- encoding : utf-8 -*-
   Rails.application.config.middleware.use OmniAuth::Builder do
     provider :wechat, Settings.wechat.app_id, Settings.wechat.app_secret
   end

4. 跟Rails集成时, 需要routes中包含: 

# config/routes.rb
get '/auth/:provider/callback', to: 'sessions#create'

这个 路由很重要, 它是 微信 验证成功, 回调后的url 

5. omni-auth, weixin的组件,会自定义一个 url,   /auth/wechat, 也就是说, 在你的h5页面上打开这个, 就会出现:    微信验证的页面.

6. 编辑你的 sessions_controller.rb: 

# -*- encoding : utf-8 -*-
class SessionsController < ApplicationController
  skip_before_action :verify_authenticity_token, :authenticate_user!

  def create
    @title = '微信登录-途铃'
    @info = auth_hash

    #request_url
    #auth/wechat

    openid = auth_hash.fetch('extra').fetch('raw_info').fetch("openid") rescue ''
    logger.info "== openid: #{openid}"
    logger.info auth_hash.inspect
    wechat_user_info = auth_hash.fetch('extra').fetch('raw_info')
    logger.info "== wechat_user_info: #{wechat_user_info}"

    @mobile_user = MobileUser.find_by_token(openid)
    if @mobile_user.present?
      redirect_to "#{Settings.h5_server}?uuid=#{openid}"
    else
      @mobile_user = MobileUser.new(:token => openid, :icon=>wechat_user_info.fetch('headimgurl'), :name=>wechat_user_info.fetch('nickna
      @mobile_user.save(:validate=>false)
      redirect_to "#{Settings.h5_server}/login/#{@mobile_user.id}"
    end 
  end 

  protected

  def auth_hash
    request.env['omniauth.auth']
  end 
end
上面的 "auth_hash" 就可以打印出:
#<OmniAuth::AuthHash credentials=#<OmniAuth::AuthHash expires=true expires_at=1468489304 
refresh_token="Fr11R2q_uA9PJVCLX25CDo5poiexTfs8CQThhFVgJbc8xnncgiy7yqPPckBTCcG5aF5jhDuU_VARsnCYRvbG_afAJQ8iu7u97zfvdBiBQU0" 
token="zCR9p2Vo_mS71q9sjs1DdjuTq-MFOrjXDcbPSbSlrqjGBWQHKxgoCWzSOEcAteyvhnCRiArnx9j6ZvSHUyHDoNfgHMCDSwwVKFB935WiUuA"> 
extra=#<OmniAuth::AuthHash raw_info=#<OmniAuth::AuthHash city="Chaoyang" 
country="CN" headimgurl="http://wx.qlogo.cn/mmopen/0p4IjkRsnlTPqRJzKQp4HlciaIK2picrtCKpQvI3LfIsMHeERZ6uUJMTSTdPkRDNzOy1IU7YGfdfkNxdwXPfVhaNnf71jZDNvy/0" 
language="zh_CN" nickname="思维" openid="oKB7ww_FSqOs1H9elPWOmGBBCRHM" privilege=[] province="Beijing" sex=1>> info=#<OmniAuth::AuthHash::InfoHash 
city="Chaoyang" country="CN" headimgurl="http://wx.qlogo.cn/mmopen/0p4IjkRsnlTPqRJzKQp4HlciaIK2picrtCKpQvI3LfIsMHeERZ6uUJMTSTdPkRDNzOy1IU7YGfdfkNxdwXPfVhaNnf71jZDNvy/0" 
nickname="思维" province="Beijing" sex=1> provider="wechat" uid="oKB7ww_FSqOs1H9elPWOmGBBCRHM">

7. 最后, 进入到微信后台,   开发 -> 接口权限 ->  网页授权 那一行 , 点击修改, 就可以修改对应的网页授权的回调url, 

Screenshot From 2016 12 03 16 38 24

填上你的服务器 地址(例如: http://your_rails.com )  就会看到这个对话框: 

Screenshot From 2016 12 03 16 35 13

8. 在这个对话框中,下载对应的文件, 放到你的远程, 保证在远程服务器的URL上是可以访问的,例如: 

http://choujiangweb.ccoymc.com/MP_verify_976gh7fBave??Ms9.txt

内容就是几个 数字. 

你的nginx配置:

server{
  location /MP_verify_ {
    root /opt/app/chou_jiang_web/current/public;
    expires 1y; 
    add_header Cache-Control public;
    add_header ETag ""; 
    break;
  }
}
然后把 MP_verify_... 这个文件放过去就可以了.

订阅/RSS Feed

Subscribe

分类/category