腾讯开放平台ruby版SDK(v3版本)
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.rvmrc
Gemfile
Gemfile.lock
MIT-LICENSE
README.md
Rakefile
open_qq.gemspec

README.md

open_qq

腾讯开放平台ruby版SDK(v3版本)

安装

gem install open_qq

使用

使用非常简单,传入应用的appid, appkey和环境地址env

require 'rubygems'
require 'open_qq'

OpenQq.setup(:appid => '123', :appkey => '456', :env => 'http://119.147.19.43')

# 或者https
OpenQq.setup(:appid => '123', :appkey => '456', :env => 'https://119.147.19.43')

# get请求
user_info = OpenQq.get('/v3/user/get_info', :openid => '111',:openkey => '222')

# 或者post请求
user_info = OpenQq.post('/v3/user/get_info',:openid => '111',:openkey => '222')

user_info.ret # => 0
user_info.nickname # => 'foo'

如果你只想原样返回未加工的数据,使用raw => true

user_info = OpenQq.post('/v3/user/get_info', {:openid => '111', :openkey => '222'}, :raw => true)
puts user_info
# => '{ "ret": 0, "is_lost": 0, "nickname": "foo" }'

如果你不想使用全局的配置

options   = {:appid => 'newappid', :appkey => 'newappkey', :env => 'http://newenv'}

user_info = OpenQq.call('/v3/user/get_info', options) do |request|
  
  request.get {:openid => '111',:openkey => '222'}

  #或者
  request.post {:openid => '111',:openkey => '222'}

end

user_info.nickname
# => 'foo'

回调协议签名验证

params = {openid: 'test001', appid: '33758', sig: 'VvKwcaMqUNpKhx0XfCvOqPRiAnU%3D'}
OpenQq.verify_callback_sig(:get, '/cgi-bin/temp.py', params)
# => true or false

#指定特定的appkey
OpenQq.verify_callback_sig(:get, '/cgi-bin/temp.py', params, 'xxxxxx')

# 在rails中使用
class OpenQqController < ApplicationController
  include OpenQq::Rails::ActionController

  def index
    if verify_callback_sig
      ...do something
    end
  end
end

在rails中使用

首先在Gemfile中添加

gem 'open_qq'

执行bundle install

在config目录下生成配置文件config/open_qq.yml

rails g open_qq:install

在配置文件中填入appid, appkey和env的值,启动服务后全局都可以使用,例如:

class OpenQqController < ApplicationController
  
  # 假设这里是应用的入口
  def index
    user_info = OpenQq.post('/v3/user/get_info', params.slice!(:action, :controller))
    if user_info.ret == 0
      # do something
    end
  end

end

注意和说明

  • 当传入的format为xml时,不会对返回的结果做处理,直接字符串返回
  • 当传入的format不为xml时,会使用JSON#parse转换成hash,并且使用OpenStruct封装
  • 当ret返回2001时,是由本api抛出
  • 关于signature verification failed,先仔细对照文档
可以通过联调工具看下签名是否一致

OpenQq.setup(:appid => '123', :appkey => '456', :env => 'http://119.147.19.43')
opts = {:openid => '1111',:openkey => '2222',:pf => 'pengyou'}
sig = OpenQq.wrap(:post, '/v3/user/get_info', opts)[:sig]

puts sig # 与联调结果比对

  • 如果不想使用open_qq.yml,只要在使用前全局配置好OpenQq即可
  • 测试基本覆盖,可以下载下来执行rake
  • bug反馈Issue

Changelog

  • 2012/09/11 增加对https的支持 支持支付回调协议签名验证

This project rocks and uses MIT-LICENSE.