View Source WeChat.Plug.OAuth2Checker (wechat v0.12.5)
网页授权
工作流程:
- 检查
session
, 判断是否已经有授权,有则继续执行后续的 plug, 没有则跳到步骤 2; - 检查
query
是否有code
, 有则执行oauth2_callback_fun
函数, 没有则跳到步骤 3; - 执行
authorize_url_fun
函数,跳转到authorize_url
.
可选参数:
:oauth2_callback_fun
: 定义, 默认值: oauth2_callback/5:authorize_url_fun
: 定义, 默认值: authorize_url_by_server_role/4
注意 : 服务器角色为 hub_client
时,请确保已经配置 hub_springboard_url
:
WeChat.HubClient.set_hub_springboard_url(Client, "https://wx.example.com")
usage
Usage
将下面的代码加到 router
里面:
pipeline :oauth2_checker do
plug WeChat.Plug.OAuth2Checker, clients: [Client, ...]
end
# for normal
scope "/:app" do
pipe_through :oauth2_checker
get "/path", YourController, :your_action
end
# for work
scope "/:app/:agent" do
pipe_through :oauth2_checker
# 在PC端授权访问网页
get "/:qr/your_path", YourController, :your_action
# 在企业微信内授权访问网页
get "/your_path", YourController, :your_action
end
Link to this section Summary
Link to this section Types
@type authorize_url_fun() :: (:normal | :work | :work_qr, Plug.Conn.t(), WeChat.client(), WeChat.Work.Agent.t() -> authorize_url :: String.t())
获取授权链接函数
@type oauth2_callback_fun() :: (:normal | :work, Plug.Conn.t(), WeChat.WebPage.code(), WeChat.client(), WeChat.Work.Agent.t() -> Plug.Conn.t())
授权回调处理函数