wechat-utils 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wechat/utils.rb +80 -42
- data/lib/wechat/utils/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23d1191d16cd76fe621247b3b14f41dac3e94af1
|
4
|
+
data.tar.gz: 9ddbcd51489bb9cb2c4860cf7236f456b026a64e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7dd18f109cd1945e4c758eea30e1f985c6bb7a3d437237c02453b319681f2c3541a9a40aba35a90ab34402e90247f485d97d0a87e08583adca5062e69d3443d6
|
7
|
+
data.tar.gz: 0d2465af9e8ef57fdb22c6cb25834693cdb60ebbac6db4f674f4a86dbec42128423835baa32c8b2a044480b567e3e5b99332e3306f184c4180167c3811321bce
|
data/lib/wechat/utils.rb
CHANGED
@@ -1,56 +1,94 @@
|
|
1
1
|
require 'rest-client'
|
2
2
|
require 'wechat/utils/version'
|
3
|
+
require 'securerandom'
|
4
|
+
|
3
5
|
|
4
6
|
module Wechat
|
5
7
|
module Utils
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
class << self
|
9
|
+
def create_oauth_url_for_code app_id, redirect_url, more_info = false, state=nil
|
10
|
+
common_parts = {
|
11
|
+
appid: app_id,
|
12
|
+
redirect_uri: CGI::escape(redirect_url),
|
13
|
+
response_type: 'code',
|
14
|
+
scope: more_info ? 'snsapi_userinfo' : 'snsapi_base',
|
15
|
+
state: state
|
16
|
+
}
|
17
|
+
"https://open.weixin.qq.com/connect/oauth2/authorize?#{hash_to_query common_parts}#wechat_redirect"
|
18
|
+
end
|
16
19
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
20
|
+
def create_oauth_url_for_openid app_id, app_secret, code
|
21
|
+
query_parts = {
|
22
|
+
appid: app_id,
|
23
|
+
secret: app_secret,
|
24
|
+
code: code,
|
25
|
+
grant_type: 'authorization_code'
|
26
|
+
}
|
27
|
+
"https://api.weixin.qq.com/sns/oauth2/access_token?#{hash_to_query query_parts}"
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
return nil, response
|
32
|
-
else
|
33
|
-
return response['openid'], nil
|
30
|
+
def fetch_openid_and_access_token app_id, app_secret, code
|
31
|
+
url = create_oauth_url_for_openid app_id, app_secret, code
|
32
|
+
response = get_request url
|
33
|
+
return response['openid'], response['access_token'], response
|
34
34
|
end
|
35
|
-
end
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
:ssl_version => 'TLSv1',
|
42
|
-
:method => 'GET',
|
43
|
-
:headers => false,
|
44
|
-
:open_timeout => 30,
|
45
|
-
:timeout => 30
|
46
|
-
}
|
47
|
-
JSON.parse RestClient::Request.execute(request_opts).body
|
48
|
-
end
|
36
|
+
# access_token is get from oauth
|
37
|
+
def fetch_oauth_user_info access_token, openid
|
38
|
+
get_request "https://api.weixin.qq.com/sns/userinfo?access_token=#{access_token}&openid=#{openid}&lang=zh_CN"
|
39
|
+
end
|
49
40
|
|
50
|
-
|
41
|
+
# access_token is the global token
|
42
|
+
def fetch_user_info access_token, openid
|
43
|
+
get_request "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN"
|
44
|
+
end
|
45
|
+
|
46
|
+
def get_request url
|
47
|
+
request_opts = {
|
48
|
+
:url => url,
|
49
|
+
:verify_ssl => false,
|
50
|
+
:ssl_version => 'TLSv1',
|
51
|
+
:method => 'GET',
|
52
|
+
:headers => false,
|
53
|
+
:open_timeout => 30,
|
54
|
+
:timeout => 30
|
55
|
+
}
|
56
|
+
JSON.parse RestClient::Request.execute(request_opts).body
|
57
|
+
end
|
51
58
|
|
52
|
-
|
53
|
-
|
59
|
+
def fetch_jsapi_ticket access_token
|
60
|
+
response = get_request "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi"
|
61
|
+
return response['ticket'], response
|
62
|
+
end
|
63
|
+
|
64
|
+
def fetch_global_access_token appid, secret
|
65
|
+
response = get_request "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}"
|
66
|
+
return response['access_token'], response
|
67
|
+
end
|
68
|
+
|
69
|
+
def jsapi_params appid, url, jsapi_ticket
|
70
|
+
timestamp = Time.now.to_i
|
71
|
+
noncestr = SecureRandom.urlsafe_base64(12)
|
72
|
+
signature = sign_params timestamp: timestamp, noncestr: noncestr, jsapi_ticket: jsapi_ticket, url: url
|
73
|
+
{
|
74
|
+
appid: appid,
|
75
|
+
timestamp: timestamp,
|
76
|
+
noncestr: noncestr,
|
77
|
+
signature: signature,
|
78
|
+
url: url
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def hash_to_query hash
|
85
|
+
hash.map { |k, v| "#{k}=#{v}" }.join('&')
|
86
|
+
end
|
87
|
+
|
88
|
+
def sign_params options
|
89
|
+
to_be_singed_string = options.sort.map { |key, value| "#{key}=#{value}" }.join("&")
|
90
|
+
Digest::SHA1.hexdigest to_be_singed_string
|
91
|
+
end
|
54
92
|
end
|
55
93
|
end
|
56
94
|
end
|
data/lib/wechat/utils/version.rb
CHANGED