Commit 5f26d3382c8792ff2cb5f881bbd7456d443f2a11
1 parent
6f119a48
Exists in
master
添加Oauth2
Showing
6 changed files
with
59 additions
and
6 deletions
Show diff stats
README.md
@@ -2,20 +2,29 @@ | @@ -2,20 +2,29 @@ | ||
2 | 2 | ||
3 | 部门、成员、标签均可以在开发环境调试 | 3 | 部门、成员、标签均可以在开发环境调试 |
4 | 4 | ||
5 | -``` | 5 | +```ruby |
6 | gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git" | 6 | gem "qy_wechat_api", git: "https://github.com/lanrion/qy_wechat_api.git" |
7 | ``` | 7 | ``` |
8 | 8 | ||
9 | -**暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请确保不要使用全局变量做client** | 9 | +**暂未对access_token做缓存处理,为了确保在开发过程不会出现token过期问题,请确保不要使用全局变量做app_client变量。** |
10 | 10 | ||
11 | ## 基本用法 | 11 | ## 基本用法 |
12 | + | ||
12 | ```ruby | 13 | ```ruby |
13 | -client = QyWechatApi::Client.new(QyWechatApi.corpid, QyWechatApi.corpsecret) | 14 | +app_client = QyWechatApi::Client.new(corpid, corpsecret) |
14 | 15 | ||
15 | # 创建自定义菜单 | 16 | # 创建自定义菜单 |
16 | # menu_json的生成方法请参考: https://github.com/lanrion/weixin_rails_middleware/wiki/DIY-menu | 17 | # menu_json的生成方法请参考: https://github.com/lanrion/weixin_rails_middleware/wiki/DIY-menu |
17 | -client.menu.create(menu_json, agent_id) | 18 | +app_client.menu.create(menu_json, agent_id) |
19 | + | ||
20 | +# Oauth 用法 | ||
21 | +# 先要配置你应用的 可信域名 2458023e.ngrok.com | ||
22 | +# state 为开发者自定义参数,可选 | ||
23 | +app_client.oauth.authorize_url("http://2458023e.ngrok.com", "state") | ||
18 | 24 | ||
25 | +# 获取code后,获取用户信息 | ||
26 | +# app_id: 跳转链接时所在的企业应用ID | ||
27 | +app_client.oauth.get_user_info("code", "app_id") | ||
19 | ``` | 28 | ``` |
20 | 29 | ||
21 | 30 |
lib/qy_wechat_api.rb
@@ -13,10 +13,12 @@ module QyWechatApi | @@ -13,10 +13,12 @@ module QyWechatApi | ||
13 | OK_CODE = 0.freeze | 13 | OK_CODE = 0.freeze |
14 | 14 | ||
15 | class << self | 15 | class << self |
16 | + # for test | ||
16 | def corpid | 17 | def corpid |
17 | "wxb9ce1d023fe6eb69" | 18 | "wxb9ce1d023fe6eb69" |
18 | end | 19 | end |
19 | 20 | ||
21 | + # for test | ||
20 | def corpsecret | 22 | def corpsecret |
21 | "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps" | 23 | "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps" |
22 | end | 24 | end |
@@ -44,5 +46,10 @@ module QyWechatApi | @@ -44,5 +46,10 @@ module QyWechatApi | ||
44 | en_msg = result_hash.delete("errmsg") | 46 | en_msg = result_hash.delete("errmsg") |
45 | ResultHandler.new(code, en_msg, result_hash) | 47 | ResultHandler.new(code, en_msg, result_hash) |
46 | end | 48 | end |
49 | + | ||
50 | + def open_endpoint(url) | ||
51 | + "https://open.weixin.qq.com#{url}" | ||
52 | + end | ||
53 | + | ||
47 | end | 54 | end |
48 | end | 55 | end |
lib/qy_wechat_api/api.rb
@@ -5,3 +5,4 @@ require "qy_wechat_api/api/message" | @@ -5,3 +5,4 @@ require "qy_wechat_api/api/message" | ||
5 | require "qy_wechat_api/api/tag" | 5 | require "qy_wechat_api/api/tag" |
6 | require "qy_wechat_api/api/user" | 6 | require "qy_wechat_api/api/user" |
7 | require "qy_wechat_api/api/menu" | 7 | require "qy_wechat_api/api/menu" |
8 | +require "qy_wechat_api/api/oauth" |
lib/qy_wechat_api/api/base.rb
@@ -3,10 +3,11 @@ | @@ -3,10 +3,11 @@ | ||
3 | module QyWechatApi | 3 | module QyWechatApi |
4 | module Api | 4 | module Api |
5 | class Base | 5 | class Base |
6 | - attr_accessor :access_token | 6 | + attr_accessor :access_token, :corp_id |
7 | 7 | ||
8 | - def initialize(access_token) | 8 | + def initialize(access_token, corp_id=nil) |
9 | @access_token = access_token | 9 | @access_token = access_token |
10 | + @corp_id = corp_id | ||
10 | end | 11 | end |
11 | 12 | ||
12 | private | 13 | private |
@@ -22,6 +23,10 @@ module QyWechatApi | @@ -22,6 +23,10 @@ module QyWechatApi | ||
22 | QyWechatApi.http_post_without_token(request_url, payload, params) | 23 | QyWechatApi.http_post_without_token(request_url, payload, params) |
23 | end | 24 | end |
24 | 25 | ||
26 | + def base_url | ||
27 | + "" | ||
28 | + end | ||
29 | + | ||
25 | end | 30 | end |
26 | end | 31 | end |
27 | end | 32 | end |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +# encoding: utf-8 | ||
2 | +module QyWechatApi | ||
3 | + module Api | ||
4 | + class Oauth < Base | ||
5 | + | ||
6 | + # appid 是 企业的CorpID | ||
7 | + # redirect_uri 是 授权后重定向的回调链接地址,请使用urlencode对链接进行处理 | ||
8 | + # response_type 是 返回类型,此时固定为:code | ||
9 | + # scope 是 应用授权作用域,此时固定为:snsapi_base | ||
10 | + # state 否 重定向后会带上state参数,企业可以填写a-zA-Z0-9的参数值 | ||
11 | + # #wechat_redirect 是微信终端使用此参数判断是否需要带上身份信息 | ||
12 | + # https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect | ||
13 | + def authorize_url(redirect_uri, state="qy_wechat") | ||
14 | + require "erb" | ||
15 | + redirect_uri = ERB::Util.url_encode(redirect_uri) | ||
16 | + QyWechatApi.open_endpoint("/connect/oauth2/authorize?appid=#{corp_id}&redirect_uri=#{redirect_uri}&response_type=code&scope=snsapi_base&state=#{state}#wechat_redirect") | ||
17 | + end | ||
18 | + | ||
19 | + # 根据code获取成员信息 | ||
20 | + # https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=ACCESS_TOKEN&code=CODE&agentid=AGENTID | ||
21 | + def get_user_info(code, agent_id) | ||
22 | + http_get("user/getuserinfo", {code: code, agentid: agent_id}) | ||
23 | + end | ||
24 | + | ||
25 | + end | ||
26 | + end | ||
27 | +end |
lib/qy_wechat_api/client.rb
@@ -38,6 +38,10 @@ module QyWechatApi | @@ -38,6 +38,10 @@ module QyWechatApi | ||
38 | Api::Menu.new(get_access_token) | 38 | Api::Menu.new(get_access_token) |
39 | end | 39 | end |
40 | 40 | ||
41 | + def oauth | ||
42 | + Api::Oauth.new(get_access_token, corp_id) | ||
43 | + end | ||
44 | + | ||
41 | private | 45 | private |
42 | def get_access_token | 46 | def get_access_token |
43 | self.access_token ||= get_token.result[:access_token] | 47 | self.access_token ||= get_token.result[:access_token] |