WeChat.Plug.WebPageOAuth2 (wechat v0.2.0)
网页授权
Usage
将下面的代码加到 router
里面:
单一应用的情况:
scope "/wx/oauth2", WeChat.Plug do get "/callback/*path", WebPageOAuth2, [client: Client, action: :oauth2_callback] get "/*path", WebPageOAuth2, [client: Client, action: :oauth2] end
多个应用的情况:
scope "/wx/oauth2/:code_name", WeChat.Plug do get "/callback/*path", WebPageOAuth2, :oauth2_callback get "/*path", WebPageOAuth2, :oauth2 end
服务器角色为
hub
:get "/wx/oauth2/:code_name/:app/callback/*path", WeChat.Plug.WebPageOAuth2, :hub_oauth2_callback
服务器角色为
hub_client
:scope "/wx/oauth2/:code_name", WeChat.Plug do get "/callback/*path", WebPageOAuth2, :oauth2_callback get "/*path", WebPageOAuth2, :hub_client_oauth2 end
用户在微信进入下面这个链接:
/wx/oauth2/:code_name/*path?xx=xx
经过 plug
之后,会跳转到微信的网页授权:
https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
用户完成授权之后,微信会跳转回 REDIRECT_URI/?code=CODE&state=STATE
,即:
/wx/oauth2/:code_name/callback/*path?xx=xx
默认的 oauth2_callback
函数拿到 query
里面的 code
换取 access_token
,
成功之后将 openid
和 appid
写入到 session
,之后跳转到路径:
/*path?xx=xx
Link to this section Summary
Link to this section Functions
Link to this function
call(conn, action)
Link to this function
hub_client_oauth2(conn, query_params, client)
Link to this function
hub_oauth2_callback(conn, query_params, client)
Link to this function
init(action)
Link to this function
oauth2(conn, query_params, client)
Link to this function
oauth2_callback(conn, options)
Link to this function