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
712de9f5
Commit
712de9f5
authored
Jul 17, 2014
by
lanrion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added oauth2 api
parent
8733c61a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
4 deletions
+20
-4
lib/weixin_authorize.rb
+5
-1
lib/weixin_authorize/api/oauth.rb
+14
-3
lib/weixin_authorize/client.rb
+1
-0
No files found.
lib/weixin_authorize.rb
View file @
712de9f5
...
@@ -47,7 +47,11 @@ module WeixinAuthorize
...
@@ -47,7 +47,11 @@ module WeixinAuthorize
end
end
def
plain_endpoint
def
plain_endpoint
"https://api.weixin.qq.com/cgi-bin"
"
#{
api_endpoint
}
/cgi-bin"
end
def
api_endpoint
"https://api.weixin.qq.com"
end
end
def
file_endpoint
def
file_endpoint
...
...
lib/weixin_authorize/api/oauth.rb
View file @
712de9f5
...
@@ -10,16 +10,27 @@ module WeixinAuthorize
...
@@ -10,16 +10,27 @@ module WeixinAuthorize
# state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值
# state 重定向后会带上state参数,开发者可以填写a-zA-Z0-9的参数值
# 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
# 如果用户点击同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,仅会带上state参数redirect_uri?state=STATE
def
authorize_url
(
redirect_uri
,
scope
=
"snsapi_base"
,
state
=
""
)
def
authorize_url
(
redirect_uri
,
scope
=
"snsapi_base"
,
state
=
"
weixin
"
)
require
"erb"
require
"erb"
ERB
::
Util
.
url_encode
(
WeixinAuthorize
.
open_endpoint
(
"/connect/oauth2/authorize?appid=
#{
app_id
}
&redirect_uri=
#{
redirect_uri
}
&response_type=code&scope=
#{
scope
}
&state=
#{
state
}
#wechat_redirect"
))
redirect_uri
=
ERB
::
Util
.
url_encode
(
redirect_uri
)
WeixinAuthorize
.
open_endpoint
(
"/connect/oauth2/authorize?appid=
#{
app_id
}
&redirect_uri=
#{
redirect_uri
}
&response_type=code&scope=
#{
scope
}
&state=
#{
state
}
#wechat_redirect"
)
end
end
# 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
# 首先请注意,这里通过code换取的网页授权access_token,与基础支持中的access_token不同。公众号可通过下述接口来获取网页授权access_token。如果网页授权的作用域为snsapi_base,则本步骤中获取到网页授权access_token的同时,也获取到了openid,snsapi_base式的网页授权流程即到此为止。
# 微信通过请求 #authorize_url 方法后,会返回一个code到redirect_uri中
# 微信通过请求 #authorize_url 方法后,会返回一个code到redirect_uri中
def
get_oauth_access_token
(
code
)
def
get_oauth_access_token
(
code
)
http_get
(
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=
#{
app_id
}
&secret=
#{
app_secret
}
&code=
#{
code
}
&grant_type=authorization_code"
)
WeixinAuthorize
.
http_get_without_token
(
"/sns/oauth2/access_token?appid=
#{
app_id
}
&secret=
#{
app_secret
}
&code=
#{
code
}
&grant_type=authorization_code"
,
{},
"api"
)
end
# refresh_token: 填写通过access_token获取到的refresh_token参数
def
refresh_oauth2_token
(
refresh_token
)
WeixinAuthorize
.
http_get_without_token
(
"/sns/oauth2/refresh_token?appid=
#{
app_id
}
&grant_type=refresh_token&refresh_token=
#{
refresh_token
}
"
)
end
# 如果网页授权作用域为snsapi_userinfo,则此时开发者可以通过access_token和openid拉取用户信息了。
def
get_oauth_userinfo
(
openid
,
oauth_token
,
lang
=
"zh_CN"
)
WeixinAuthorize
.
http_get_without_token
(
"/sns/userinfo?access_token=
#{
oauth_token
}
&openid=
#{
openid
}
&lang=
#{
lang
}
"
)
end
end
private
private
...
...
lib/weixin_authorize/client.rb
View file @
712de9f5
...
@@ -10,6 +10,7 @@ module WeixinAuthorize
...
@@ -10,6 +10,7 @@ module WeixinAuthorize
include
Api
::
Groups
include
Api
::
Groups
include
Api
::
Qrcode
include
Api
::
Qrcode
include
Api
::
Media
include
Api
::
Media
include
Api
::
Oauth
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:app_id
,
:app_secret
,
:expired_at
# Time.now + expires_in
attr_accessor
:access_token
,
:redis_key
attr_accessor
:access_token
,
:redis_key
...
...
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