Skip to content
Wechat OAuth2 Strategy for OmniAuth 1.0.
Branch: master
Clone or download
Latest commit 6f138c1 Mar 18, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib Merge pull request #9 from simoniong/fix_authorize_url Mar 18, 2019
spec Merge pull request #9 from simoniong/fix_authorize_url Mar 18, 2019
.gitignore initial commit Mar 27, 2014
.travis.yml removed 1.9.3 version support Mar 15, 2019
Gemfile
LICENSE
README.md Update README.md Mar 15, 2019
Rakefile initial commit Mar 27, 2014
VERSION release 0.1.1 Mar 15, 2019
omniauth-wechat-oauth2.gemspec added maintainer Mar 18, 2019

README.md

Omniauth-wechat-oauth2

Gem Version Security Check Build Status

Wechat OAuth2 Strategy for OmniAuth 1.0.

You need to get a wechat API key at: https://mp.weixin.qq.com

Wechat oauth2 specification can be found at: https://mp.weixin.qq.com/wiki/index.php?title=网页授权获取用户基本信息

Installation

Add to your Gemfile:

gem "omniauth-wechat-oauth2"

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"
        }
    }
}
You can’t perform that action at this time.