Commit 2dfb2050 by lanrion

Merge branch 'master' into broadcast-apis

Conflicts:
	lib/weixin_authorize/api.rb
	lib/weixin_authorize/client.rb
	spec/spec_helper.rb
parents 42314e24 5ef7b637
...@@ -2,7 +2,7 @@ source 'https://rubygems.org' ...@@ -2,7 +2,7 @@ source 'https://rubygems.org'
group :test, :development do group :test, :development do
gem "rspec", "~> 3.0.0.beta1" gem "rspec", "~> 3.0.0.beta1"
gem "redis-namespace", "~> 1.4.1" gem 'redis-namespace', '~> 1.5.1'
gem "rake", "~> 0.9.6" gem "rake", "~> 0.9.6"
gem 'simplecov', '~> 0.7.1', :require => false gem 'simplecov', '~> 0.7.1', :require => false
gem "codeclimate-test-reporter", require: nil gem "codeclimate-test-reporter", require: nil
......
...@@ -47,7 +47,11 @@ module WeixinAuthorize ...@@ -47,7 +47,11 @@ module WeixinAuthorize
end end
def plain_endpoint def plain_endpoint
"https://api.weixin.qq.com/cgi-bin" "#{api_endpoint}/cgi-bin"
end
def api_endpoint
"https://api.weixin.qq.com"
end end
def file_endpoint def file_endpoint
...@@ -58,6 +62,10 @@ module WeixinAuthorize ...@@ -58,6 +62,10 @@ module WeixinAuthorize
"https://mp.weixin.qq.com/cgi-bin#{url}" "https://mp.weixin.qq.com/cgi-bin#{url}"
end end
def open_endpoint(url)
"https://open.weixin.qq.com#{url}"
end
end end
end end
...@@ -5,3 +5,4 @@ require "weixin_authorize/api/groups" ...@@ -5,3 +5,4 @@ require "weixin_authorize/api/groups"
require "weixin_authorize/api/qrcode" require "weixin_authorize/api/qrcode"
require "weixin_authorize/api/media" require "weixin_authorize/api/media"
require "weixin_authorize/api/mass" require "weixin_authorize/api/mass"
require "weixin_authorize/api/oauth"
# encoding: utf-8
module WeixinAuthorize
module Api
module Oauth
# 应用授权作用域: scope
# snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
# snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
# default is snsapi_base
# state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值
# 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
def authorize_url(redirect_uri, scope="snsapi_base", state="weixin")
require "erb"
redirect_uri = ERB::Util.url_encode(redirect_uri)
WeixinAuthorize.open_endpoint("/connect/oauth2/authorize?appid=#{app_id}&redirect_uri=#{redirect_uri}&response_type=code&scope=#{scope}&state=#{state}#wechat_redirect")
end
# 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
# 微信通过请求 #authorize_url 方法后,会返回一个code到redirect_uri中
def get_oauth_access_token(code)
WeixinAuthorize.http_get_without_token("/sns/oauth2/access_token?appid=#{app_id}&secret=#{app_secret}&code=#{code}&grant_type=authorization_code", {}, "api")
end
# refresh_token: 填写通过access_token获取到的refresh_token参数
def refresh_oauth2_token(refresh_token)
WeixinAuthorize.http_get_without_token("/sns/oauth2/refresh_token?appid=#{app_id}&grant_type=refresh_token&refresh_token=#{refresh_token}", {}, "api")
end
# 如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
def get_oauth_userinfo(openid, oauth_token, lang="zh_CN")
WeixinAuthorize.http_get_without_token("/sns/userinfo?access_token=#{oauth_token}&openid=#{openid}&lang=#{lang}", {}, "api")
end
private
end
end
end
\ No newline at end of file
...@@ -11,6 +11,7 @@ module WeixinAuthorize ...@@ -11,6 +11,7 @@ module WeixinAuthorize
include Api::Qrcode include Api::Qrcode
include Api::Media include Api::Media
include Api::Mass include Api::Mass
include Api::Oauth
attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in attr_accessor :app_id, :app_secret, :expired_at # Time.now + expires_in
attr_accessor :access_token, :redis_key attr_accessor :access_token, :redis_key
......
...@@ -19,7 +19,7 @@ describe WeixinAuthorize::Api::Media do ...@@ -19,7 +19,7 @@ describe WeixinAuthorize::Api::Media do
end end
let(:remote_png_path) do let(:remote_png_path) do
"http://l.ruby-china.org/user/large_avatar/273.png" "https://ruby-china-files.b0.upaiyun.com/user/big_avatar/273.jpg"
end end
let(:remote_jpg_path) do let(:remote_jpg_path) do
......
...@@ -37,6 +37,7 @@ SimpleCov.start ...@@ -37,6 +37,7 @@ SimpleCov.start
ENV['CODECLIMATE_REPO_TOKEN'] = "c91fecbbd9e414e7cc3ad7a7d99207145de0ac65a3368de09e8c19295343d399" ENV['CODECLIMATE_REPO_TOKEN'] = "c91fecbbd9e414e7cc3ad7a7d99207145de0ac65a3368de09e8c19295343d399"
CodeClimate::TestReporter.start CodeClimate::TestReporter.start
# If you want test, change your weixin test profile
ENV["APPID"]="wx986f04063d341d04" ENV["APPID"]="wx986f04063d341d04"
ENV["APPSECRET"]="1a941cd88cb4579ba98ec06b6813af03" ENV["APPSECRET"]="1a941cd88cb4579ba98ec06b6813af03"
ENV["OPENID"]="o9k6BuB0kydAcPTc7sPxppB1GQqA" ENV["OPENID"]="o9k6BuB0kydAcPTc7sPxppB1GQqA"
......
...@@ -19,7 +19,7 @@ Gem::Specification.new do |spec| ...@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"] spec.require_paths = ["lib"]
spec.add_dependency "rest-client", ">= 1.6.7" spec.add_dependency "rest-client", ">= 1.6.7"
spec.add_dependency "redis", "~> 3.0.7" spec.add_dependency "redis", "~> 3.1.0"
spec.add_dependency "carrierwave", "~> 0.10.0" spec.add_dependency "carrierwave", "~> 0.10.0"
spec.add_dependency 'mini_magick', '~> 3.7.0' spec.add_dependency 'mini_magick', '~> 3.7.0'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment