Giter Site home page Giter Site logo

wechat's Introduction

SimpleWechat

Build Status

Build Status

Code Climate

Test Coverage

Simple Wechat Api Wrapper.

Installation

Add this line to your application's Gemfile:

gem 'simple_wechat'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_wechat

Usage

Fetch access token

client = SimpleWechat::Client.new("APPID", "APPSECRET")
access_token = client.get_access_token  
# access_token and expires_in are wrapped in access_token.

Fetch jsapi ticket

jsapi = SimpleWechat::JsApi.new
ticket = jsapi.get_ticket("ACCESS_TOKEN")
# ticket, errcode, errmsg, expires_in are wrapped in ticket.

Get jsapi config

ticket = "sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg"
jsapi = SimpleWechat::JsApi.new
config = jsapi.get_config(ticket, "http://wx.qq.com", "APPID", ["previewImage"])
# now MultiJson.encode(config) can be passed into function wx.config() in javascript.

OAuth

OAuth part is borrowed from wechat-auth_client ( https://github.com/zhangyuan/wechat-auth_client ).

REDIRECT_URL should be configured in WeChat management system and route to callback action

class SessionsController < ApplicationController
  def auth
    redirect_to auth_client.authorize_url(REDIRECT_URL, state)
  end

  def callback
    if state == params[:state]
      access_token = auth_client.get_token(params[:code])
      # do something with access_token. 
      # for example, call access_token.openid to get openid
    end
  end

  private

  def auth_client
    @auth_client ||= SimpleWechat::Client.new(APP_ID, APP_SECRET).get_auth_client
  end
  
  def state
    session[:state] ||= SecureRandom.hex(3)
  end
end

Contributing

  1. Fork it ( https://github.com/zhangyuan/wechat/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

wechat's People

Contributors

zhangyuan avatar

Stargazers

zhangzx avatar Feng XinGuo avatar Alexander ADAM avatar Ryan Wong avatar Alexey Tsverov avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

lazing

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.