-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathagent.rb
More file actions
63 lines (55 loc) · 2.1 KB
/
agent.rb
File metadata and controls
63 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require "erb"
module Wework
module Api
module Methods
module Agent
def authorize_url(redirect_uri, scope="snsapi_base", state="wxwork")
# user agent: UA is mozilla/5.0 (iphone; cpu iphone os 10_2 like mac os x) applewebkit/602.3.12 (khtml, like gecko) mobile/14c92 wxwork/1.3.2 micromessenger/6.2
uri = ERB::Util.url_encode(redirect_uri)
"#{AUTHORIZE_ENDPOINT}?appid=#{corp_id}&redirect_uri=#{uri}&response_type=code&scope=#{scope}&agentid=#{agent_id}&state=#{state}#wechat_redirect"
end
def get_oauth_userinfo code
get 'user/getuserinfo', params: {code: code}
end
def get_user_detail user_ticket
post 'user/getuserdetail', {user_ticket: user_ticket}
end
def get_jssign_package url
timestamp = Time.now.to_i
noncestr = SecureRandom.hex(8)
str = "jsapi_ticket=#{jsapi_ticket}&noncestr=#{noncestr}×tamp=#{timestamp}&url=#{url}"
{
"appId" => corp_id,
"nonceStr" => noncestr,
"timestamp" => timestamp,
"url" => url,
"signature" => Digest::SHA1.hexdigest(str),
"rawString" => str
}
end
def get_agent_jssign_package url
timestamp = Time.now.to_i
noncestr = SecureRandom.hex(8)
str = "jsapi_ticket=#{jsapi_agent_ticket}&noncestr=#{noncestr}×tamp=#{timestamp}&url=#{url}"
{
"appId" => corp_id,
"nonceStr" => noncestr,
"timestamp" => timestamp,
"url" => url,
"signature" => Digest::SHA1.hexdigest(str),
"rawString" => str
}
end
def get_session_with_jscode(js_code, grant_type='authorization_code')
post 'miniprogram/jscode2session', {}, params: {js_code: js_code, grant_type: grant_type}
end
def get_agent
get 'agent/get', params: {agentid: agent_id}
end
def set_agent data={}
post 'agent/set', data.merge(agentid: agent_id)
end
end
end
end
end