WeChat Media Platform Authentication

Build Status Coverage Status Hex Version License

An Elixir WeChat Media Platform Authentication Client Library 微信第三方平台授权

Installation

  1. Add wechat_mp_auth to your list of dependencies in mix.exs:

    def deps do
      [{:wechat_mp_auth, "~> 0.0.2"}]
    end
  2. Ensure wechat_mp_auth is started before your application:

    def application do
      [applications: [:wechat_mp_auth]]
    end

Usage

  1. Initialize a client with client_id, client_secret, and redirect_uri.
  client = WechatMPAuth.Client.new([
    strategy: WechatMPAuth.Strategy.AuthCode, #default
    client_id: "client_id",
    client_secret: "abc123",
    redirect_uri: "https://example.com/auth/callback"
  ])
  1. Use get_authorize_url to generate:

    • the authorization URL using component_verify_ticket received from WeChat
    • client that contains component_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"
  1. After authorizing from the above URL, server redirects to redirect_uri with query params: authorization_code and expires_in (https://example.com/auth/callback?auth_code=@@@&expires_in=600). Use component_access_token and authorization_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"])
  1. Use component_access_token to make a request for resources.
  resource = WechatMPAuth.ComponentAccessToken.get!(token, "/api_get_authorizer_info").body

License

Please see LICENSE for licensing details.