An Elixir WeChat Media Platform Authentication Client Library 微信第三方平台授权
-
Add wechat_mp_auth to your list of dependencies in
mix.exs
:def deps do [{:wechat_mp_auth, "~> 0.0.3"}] end
-
Ensure wechat_mp_auth is started before your application:
def application do [applications: [:wechat_mp_auth]] end
- Initialize a client with
client_id
,client_secret
, andredirect_uri
.
client = WechatMPAuth.Client.new([
strategy: WechatMPAuth.Strategy.AuthCode, #default
client_id: "client_id",
client_secret: "abc123",
redirect_uri: "https://example.com/auth/callback"
])
- Use
get_authorize_url
to generate: - the authorization URL usingcomponent_verify_ticket
received from WeChat - client that containscomponent_access_token
{client, url} = WechatMPAuth.Client.get_authorize_url(client, [verify_ticket: verify_ticket])
# component_access_token => `client.params["component_access_token"]`
# authorization URL => "https://mp.weixin.qq.com/cgi-bin/componentloginpage?component_appid=client_id&pre_auth_code=preauthcode@@@xxx&redirect_uri=https://example.com/auth/callback"
- After authorizing from the above URL, server redirects to
redirect_uri
with query params:authorization_code
andexpires_in
(https://example.com/auth/callback?auth_code=@@@&expires_in=600). Usecomponent_access_token
andauthorization_code
to get authorizer access token.
{:ok, authorizer_access_token} = client |> WechatMPAuth.Client.get_authorizer_access_token([authorization_code: authorization_code, component_access_token: "component-access-token"])
- Use
component_access_token
to make a request for resources.
resource = WechatMPAuth.ComponentAccessToken.post!(token, "/component/api_get_authorizer_info", %{component_appid: "xxx", authorizer_appid: "xxx"}).body
Please see LICENSE for licensing details.