No commit activity in last 3 years
No release in over 3 years
Using OAuth2 to authenticate wechat user when web resources being viewed within wechat(weixin) client.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.7

Runtime

 Project Readme

Installation

Add to your Gemfile:

gem "omniauth_oauth2_wechat"

Then bundle install.

Usage

Here's an example for adding the middleware to a Rails app in config/initializers/omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"]
end

You can now access the OmniAuth Wechat OAuth2 URL: /auth/wechat

Configuration

You can configure several options, which you pass in to the provider method via a hash:

  • scope: Default is "snsapi_userinfo". It can either be snsapi_base or snsapi_userinfo. When scope is "snsapi_userinfo", after wechat user is authenticated, app can query userinfo using the acquired access_token.

For devise user, you can set up scope in your devise.rb as following.

config.omniauth :wechat, ENV["WECHAT_APP_ID"], ENV["WECHAT_APP_SECRET"],
    :authorize_params => {:scope => "snsapi_base"}

Auth Hash

Here's an example of an authentication hash available in the callback by accessing request.env["omniauth.auth"]:

{
    :provider => "wechat",
    :uid => "123456789",
    :info => {
      nickname:   "Nickname",
      sex:        1,
      province:   "Changning",
      city:       "Shanghai",
      country:    "China",
      headimgurl: "http://image_url"
    },
    :credentials => {
        :token => "token",
        :refresh_token => "another_token",
        :expires_at => 7200,
        :expires => true
    },
    :extra => {
        :raw_info => {
          openid:     "openid"
          nickname:   "Nickname",
          sex:        1,
          province:   "Changning",
          city:       "Shanghai",
          country:    "China",
          headimgurl: "http://image_url"
        }
    }
}