Commit 1aef119f77c04a58b116018c1d0e0e3fc410a4f3
1 parent
9fa6352e
Exists in
master
添加自定义access_token
Showing
2 changed files
with
15 additions
and
3 deletions
Show diff stats
README.md
... | ... | @@ -51,6 +51,15 @@ group_client = QyWechatApi::Client.new(corpid, corpsecret) |
51 | 51 | group_client.is_valid? |
52 | 52 | ``` |
53 | 53 | |
54 | +如果需要使用通过第三方应用 **获取企业号access_token** API 获取的 access_token | |
55 | +做如下处理: | |
56 | + | |
57 | +```ruby | |
58 | +options = {access_token: "access_token"} | |
59 | +# redis_key 也可定制 | |
60 | +group_client = QyWechatApi::Client.new(corpid, corpsecret, options) | |
61 | +``` | |
62 | + | |
54 | 63 | ## 部门 |
55 | 64 | |
56 | 65 | ```ruby | ... | ... |
lib/qy_wechat_api/client.rb
... | ... | @@ -3,17 +3,20 @@ |
3 | 3 | module QyWechatApi |
4 | 4 | class Client |
5 | 5 | attr_accessor :corp_id, :group_secret, :expired_at # Time.now + expires_in |
6 | - attr_accessor :access_token, :redis_key, :storage | |
6 | + attr_accessor :access_token, :redis_key, :storage, :custom_access_token | |
7 | 7 | |
8 | - def initialize(corp_id, group_secret, redis_key=nil) | |
8 | + def initialize(corp_id, group_secret, options={}) | |
9 | + redis_key = options[:redis_key] | |
10 | + @custom_access_token = options[:custom_access_token] | |
9 | 11 | @corp_id = corp_id |
10 | 12 | @group_secret = group_secret |
11 | - @redis_key = security_redis_key((redis_key || "qy_" + group_secret)) | |
13 | + @redis_key = security_redis_key((redis_key || "qy_#{group_secret}")) | |
12 | 14 | @storage = Storage.init_with(self) |
13 | 15 | end |
14 | 16 | |
15 | 17 | # return token |
16 | 18 | def get_access_token |
19 | + return custom_access_token if custom_access_token.present? | |
17 | 20 | @storage.access_token |
18 | 21 | end |
19 | 22 | ... | ... |