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 | 2 | |
3 | 3 | 部门、成员、标签均可以在开发环境调试 |
4 | 4 | |
5 | -``` | |
5 | +```ruby | |
6 | 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 | 13 | ```ruby |
13 | -client = QyWechatApi::Client.new(QyWechatApi.corpid, QyWechatApi.corpsecret) | |
14 | +app_client = QyWechatApi::Client.new(corpid, corpsecret) | |
14 | 15 | |
15 | 16 | # 创建自定义菜单 |
16 | 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 | 13 | OK_CODE = 0.freeze |
14 | 14 | |
15 | 15 | class << self |
16 | + # for test | |
16 | 17 | def corpid |
17 | 18 | "wxb9ce1d023fe6eb69" |
18 | 19 | end |
19 | 20 | |
21 | + # for test | |
20 | 22 | def corpsecret |
21 | 23 | "UOofFIah4PVLmkG8xMH3lpDxj6NTnQSKMrFt-HubiPB4kjB09EmTVcUjgNeermps" |
22 | 24 | end |
... | ... | @@ -44,5 +46,10 @@ module QyWechatApi |
44 | 46 | en_msg = result_hash.delete("errmsg") |
45 | 47 | ResultHandler.new(code, en_msg, result_hash) |
46 | 48 | end |
49 | + | |
50 | + def open_endpoint(url) | |
51 | + "https://open.weixin.qq.com#{url}" | |
52 | + end | |
53 | + | |
47 | 54 | end |
48 | 55 | end | ... | ... |
lib/qy_wechat_api/api.rb
lib/qy_wechat_api/api/base.rb
... | ... | @@ -3,10 +3,11 @@ |
3 | 3 | module QyWechatApi |
4 | 4 | module Api |
5 | 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 | 9 | @access_token = access_token |
10 | + @corp_id = corp_id | |
10 | 11 | end |
11 | 12 | |
12 | 13 | private |
... | ... | @@ -22,6 +23,10 @@ module QyWechatApi |
22 | 23 | QyWechatApi.http_post_without_token(request_url, payload, params) |
23 | 24 | end |
24 | 25 | |
26 | + def base_url | |
27 | + "" | |
28 | + end | |
29 | + | |
25 | 30 | end |
26 | 31 | end |
27 | 32 | end | ... | ... |
... | ... | @@ -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