Skip to content

The Wechat SNS Library is a code base to call the Wechat SNS APIs.

License

Notifications You must be signed in to change notification settings

topbitdu/wechat-sns

Repository files navigation

Wechat SNS 微信社交库

Documentation License

Gem Version Dependency Status

Wechat SNS library is a low level library for Wechat SNS APIs. 微信社交库是一个调用微信社交API的低层库。

Recent Update

Check out the Road Map to find out what's the next. Check out the Change Log to find out what's new.

Installation

Add this line to your application's Gemfile:

gem 'wechat-sns'

And then execute:

$ bundle

Or install it yourself as:

$ gem install wechat-sns

Usage

Generate an Authorize URL and Redirect 第一步:用户同意授权,获取code

redirect_to Wechat::SNS::AuthorizeLink.create(Rails.application.secrets.wechat_app_id, 'http://product.company.com/promotion/page.html')

Get Access Token per Code 第二步:通过code换取网页授权access_token

response = Wechat::SNS::AccessToken.create Rails.application.secrets.wechat_app_id, Rails.application.secrets.wechat_app_secret, params[:code]
if response.present?
  access_token  = response['access_token']
  expires_in    = response['expires_in']
  refresh_token = response['refresh_token']
  open_id       = response['openid']
  scope         = response['scope']
  union_id      = response['unionid']
end

Refresh Access Token 第三步:刷新access_token(如果需要)

response = Wechat::SNS::AccessToken.update Rails.application.secrets.wechat_app_id, refresh_token
if response.present?
  access_token  = response['access_token']
  expires_in    = response['expires_in']
  refresh_token = response['refresh_token']
  open_id       = response['openid']
  scope         = response['scope']
end

Get User Profile per Access Token 第四步:拉取用户信息(需scope为 snsapi_userinfo)

response = Wechat::SNS::UserProfile.load access_token, open_id
if response.present? && response['errcode'].blank?
  open_id        = response['openid']
  nick_name      = response['nickname']
  sex            = response['sex']
  city           = response['city']
  province       = response['province']
  country        = response['country']
  head_image_url = response['headimgurl']
  privilege      = response['privilege']
  union_id       = response['unionid']
end

Validate Access Token 附:检验授权凭证(access_token)是否有效

response = Wechat::SNS::AccessToken.load access_token, open_id
if response.present? && 0==response['errcode']
  # valid, do something more with the access_token
  Wechat::SNS::UserProfile.load access_token, open_id
else
  # invalid, the access_token should be updated
  Wechat::SNS::AccessToken.update Rails.application.secrets.wechat_app_id, refresh_token
end

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/topbitdu/wechat-sns. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

The Wechat SNS Library is a code base to call the Wechat SNS APIs.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published