Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
W
weixin_authorize
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
weixin_authorize
Commits
e1e5da83
Commit
e1e5da83
authored
Mar 14, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
can get access_token for weixin
parent
2c6a522c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
49 additions
and
5 deletions
+49
-5
lib/weixin_authorize.rb
+1
-0
lib/weixin_authorize/client.rb
+34
-3
spec/fetch_access_token_spec.rb
+14
-2
No files found.
lib/weixin_authorize.rb
View file @
e1e5da83
require
"rest-client"
require
"weixin_authorize/version"
require
"weixin_authorize/client"
...
...
lib/weixin_authorize/client.rb
View file @
e1e5da83
# encoding: utf-8
module
WeixinAuthorize
class
Client
attr_accessor
:app_id
,
:app_secret
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:access_token
def
initialize
(
&
block
)
instance_eval
(
&
block
)
def
initialize
(
app_id
=
""
,
app_secret
=
""
,
expired_at
=
nil
)
@app_id
=
app_id
@app_secret
=
app_secret
@expired_at
=
(
expired_at
.
to_i
||
Time
.
now
.
to_i
)
yield
self
if
block_given?
end
# return token
def
get_access_token
# 如果当前token过期时间小于现在的时间,则重新获取一次
if
@expired_at
<
Time
.
now
.
to_i
authenticate
end
@access_token
end
# authenticate access_token
def
authenticate
hash_infos
=
JSON
.
parse
(
RestClient
.
get
(
authenticate_url
))
self
.
access_token
=
hash_infos
[
"access_token"
]
self
.
expired_at
=
Time
.
now
.
to_i
+
hash_infos
[
"expires_in"
]
end
private
def
authenticate_url
"
#{
endpoint
}
/token?grant_type=client_credential&appid=
#{
app_id
}
&secret=
#{
app_secret
}
"
end
def
endpoint
"https://api.weixin.qq.com/cgi-bin"
end
end
end
spec/fetch_access_token_spec.rb
View file @
e1e5da83
...
...
@@ -2,8 +2,20 @@ require "spec_helper"
describe
WeixinAuthorize
::
Client
do
describe
"#get access_token"
do
it
"returns a access_token value"
do
expect
(
$client
.
app_id
).
not_to
eq
(
"ss"
)
it
"return a access_token nil value before authenticate"
do
expect
(
$client
.
access_token
).
to
eq
(
nil
)
end
it
"return access_token after authenticate"
do
$client
.
authenticate
expect
(
$client
.
access_token
).
not_to
eq
(
nil
)
end
it
"return the same access_token in the same thing twice"
do
access_token_1
=
$client
.
get_access_token
sleep
5
access_token_2
=
$client
.
get_access_token
expect
(
access_token_1
).
to
eq
(
access_token_2
)
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment