Module: Wechat::Utils
- Defined in:
- lib/wechat/utils.rb,
lib/wechat/utils/version.rb
Constant Summary collapse
- VERSION =
'0.2.0'
Class Method Summary collapse
- .create_oauth_url_for_code(app_id, redirect_url, more_info = false, state = nil) ⇒ Object
- .create_oauth_url_for_openid(app_id, app_secret, code) ⇒ Object
- .fetch_global_access_token(appid, secret, request_opts: {}) ⇒ Object
- .fetch_jsapi_ticket(access_token, request_opts: {}) ⇒ Object
-
.fetch_oauth_user_info(access_token, openid, request_opts: {}) ⇒ Object
access_token is get from oauth.
- .fetch_openid_and_access_token(app_id, app_secret, code, request_opts: {}) ⇒ Object
-
.fetch_user_info(access_token, openid, request_opts: {}) ⇒ Object
access_token is the global token.
- .get_request(url, extra_opts) ⇒ Object
- .jsapi_params(appid, url, jsapi_ticket) ⇒ Object
Class Method Details
.create_oauth_url_for_code(app_id, redirect_url, more_info = false, state = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/wechat/utils.rb', line 10 def create_oauth_url_for_code app_id, redirect_url, more_info = false, state=nil common_parts = { appid: app_id, redirect_uri: CGI::escape(redirect_url), response_type: 'code', scope: more_info ? 'snsapi_userinfo' : 'snsapi_base', state: state } "https://open.weixin.qq.com/connect/oauth2/authorize?#{hash_to_query common_parts}#wechat_redirect" end |
.create_oauth_url_for_openid(app_id, app_secret, code) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/wechat/utils.rb', line 21 def create_oauth_url_for_openid app_id, app_secret, code query_parts = { appid: app_id, secret: app_secret, code: code, grant_type: 'authorization_code' } "https://api.weixin.qq.com/sns/oauth2/access_token?#{hash_to_query query_parts}" end |
.fetch_global_access_token(appid, secret, request_opts: {}) ⇒ Object
64 65 66 67 |
# File 'lib/wechat/utils.rb', line 64 def fetch_global_access_token appid, secret, request_opts: {} response = get_request "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=#{appid}&secret=#{secret}", request_opts return response['access_token'], response end |
.fetch_jsapi_ticket(access_token, request_opts: {}) ⇒ Object
59 60 61 62 |
# File 'lib/wechat/utils.rb', line 59 def fetch_jsapi_ticket access_token, request_opts: {} response = get_request "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=#{access_token}&type=jsapi", request_opts return response['ticket'], response end |
.fetch_oauth_user_info(access_token, openid, request_opts: {}) ⇒ Object
access_token is get from oauth
38 39 40 |
# File 'lib/wechat/utils.rb', line 38 def fetch_oauth_user_info access_token, openid, request_opts: {} get_request "https://api.weixin.qq.com/sns/userinfo?access_token=#{access_token}&openid=#{openid}&lang=zh_CN", request_opts end |
.fetch_openid_and_access_token(app_id, app_secret, code, request_opts: {}) ⇒ Object
31 32 33 34 35 |
# File 'lib/wechat/utils.rb', line 31 def fetch_openid_and_access_token app_id, app_secret, code, request_opts: {} url = create_oauth_url_for_openid app_id, app_secret, code response = get_request url, request_opts return response['openid'], response['access_token'], response end |
.fetch_user_info(access_token, openid, request_opts: {}) ⇒ Object
access_token is the global token
43 44 45 |
# File 'lib/wechat/utils.rb', line 43 def fetch_user_info access_token, openid, request_opts: {} get_request "https://api.weixin.qq.com/cgi-bin/user/info?access_token=#{access_token}&openid=#{openid}&lang=zh_CN", request_opts end |
.get_request(url, extra_opts) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/wechat/utils.rb', line 47 def get_request url, extra_opts request_opts = { :url => url, :verify_ssl => false, :ssl_version => 'TLSv1', :method => 'GET', :headers => false, :timeout => 30 }.merge(extra_opts) JSON.parse RestClient::Request.execute(request_opts).body end |
.jsapi_params(appid, url, jsapi_ticket) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/wechat/utils.rb', line 69 def jsapi_params appid, url, jsapi_ticket = Time.now.to_i noncestr = SecureRandom.urlsafe_base64(12) signature = sign_params timestamp: , noncestr: noncestr, jsapi_ticket: jsapi_ticket, url: url { appid: appid, timestamp: , noncestr: noncestr, signature: signature, url: url } end |