WeChat Media Platform Authentication
An Elixir WeChat Media Platform Authentication Client Library 微信第三方平台授权
Installation
Add wechat_mp_auth to your list of dependencies in
mix.exs:def deps do
[{:wechat_mp_auth, "~> 0.0.2"}]end
Ensure wechat_mp_auth is started before your application:
def application do
[applications: [:wechat_mp_auth]]end
Usage
-
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_urlto generate:-
the authorization URL using
component_verify_ticketreceived from WeChat -
client that contains
component_access_token
-
the authorization URL using
{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_uriwith query params:authorization_codeandexpires_in(https://example.com/auth/callback?auth_code=@@@&expires_in=600). Usecomponent_access_tokenandauthorization_codeto 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_tokento make a request for resources.
resource = WechatMPAuth.ComponentAccessToken.get!(token, "/api_get_authorizer_info").bodyLicense
Please see LICENSE for licensing details.