wechat_mp_auth v0.0.2 WechatMPAuth.ComponentAccessToken

This module defines the WechatMPAuth.ComponentAccessToken struct and provides functionality to make authorized requests to an WechatMPAuth provider using the ComponentAccessToken returned by the provider.

The WechatMPAuth.ComponentAccessToken struct is created for you when you use the WechatMPAuth.Client.get_component_access_token

Notes

  • If a full url is given (e.g. “http://www.example.com/api/resource”) then it will use that otherwise you can specify an endpoint (e.g. “/api/resource”) and it will append it to the Client.site.

Examples

token =  WechatMPAuth.ComponentAccessToken.new("abc123", %WechatMPAuth.Client{site: "www.example.com"})
case WechatMPAuth.ComponentAccessToken.get(token, "/some/resource") do
  {:ok, %WechatMPAuth.Response{status_code: 401}} ->
    "Not Good"
  {:ok, %WechatMPAuth.Response{status_code: status_code, body: body}} when status_code in [200..299] ->
    "Yay!!"
  {:error, %WechatMPAuth.Error{reason: reason}} ->
    reason
end
response = WechatMPAuth.ComponentAccessToken.get!(token, "/some/resource")
response = WechatMPAuth.ComponentAccessToken.post!(token, "/some/other/resources", %{foo: "bar"})

Summary

Functions

Makes a GET request to the given URL using the WechatMPAuth.ComponentAccessToken

Same as get/2 but returns a WechatMPAuth.Response or WechatMPAuth.Error exception if the request results in an error

Same as new/2 except that the first arg is a map. Note if giving a map, please be sure to make the key a string no an atom. This is used by WechatMPAuth.Client.get_component_access_token/2 to create the WechatMPAuth.ComponentAccessToken struct

Makes a POST request to the given URL using the WechatMPAuth.ComponentAccessToken

Same as post/3 but returns a WechatMPAuth.Response or WechatMPAuth.Error exception if the request results in an error. An WechatMPAuth.Error exception is raised if the request results in an error tuple ({:error, reason})

Makes a request of given type to the given URL using the WechatMPAuth.ComponentAccessToken

Same as request/4 but returns WechatMPAuth.Response or raises an error if an error occurs during the request. An WechatMPAuth.Error exception is raised if the request results in an error tuple ({:error, reason})

Types

access_token :: binary
body :: binary | %{}
expires_at :: integer
t :: %WechatMPAuth.ComponentAccessToken{access_token: access_token, client: WechatMPAuth.Client.t, expires_at: expires_at}

Functions

get(token, url)

Specs

get(t, binary) ::
  {:ok, Response.t} |
  {:error, Error.t}

Makes a GET request to the given URL using the WechatMPAuth.ComponentAccessToken.

get!(token, url)

Specs

get!(t, binary) :: Response.t | Error.t

Same as get/2 but returns a WechatMPAuth.Response or WechatMPAuth.Error exception if the request results in an error.

new(token, client)

Specs

new(binary, WechatMPAuth.Client.t) :: t

Same as new/2 except that the first arg is a map. Note if giving a map, please be sure to make the key a string no an atom. This is used by WechatMPAuth.Client.get_component_access_token/2 to create the WechatMPAuth.ComponentAccessToken struct.

Example

iex(1)> WechatMPAuth.ComponentAccessToken.new("clientId", %WechatMPAuth.Client{})
%WechatMPAuth.ComponentAccessToken{access_token: "clientId", appid: "",
 client: %WechatMPAuth.Client{authorize_url: "https://mp.weixin.qq.com/cgi-bin/componentloginpage",
 authorizer_access_token_url: "/component/api_query_auth",
 client_id: "clientId", client_secret: "d1aifhuds6721637jahfjv76xh6sgc2",
 component_access_token_url: "/component/api_component_token", params: %{},
 pre_auth_code_url: "/component/api_create_preauthcode",
 redirect_uri: "http://example.com/weixin_callback", site: "",
 strategy: WechatMPAuth.Strategy.AuthCode}, expires_at: nil,
 refresh_token: ""}
post(token, url, body \\ "")

Specs

post(t, binary, body) ::
  {:ok, Response.t} |
  {:error, Error.t}

Makes a POST request to the given URL using the WechatMPAuth.ComponentAccessToken.

post!(token, url, body \\ "")

Specs

post!(t, binary, body) :: Response.t | Error.t

Same as post/3 but returns a WechatMPAuth.Response or WechatMPAuth.Error exception if the request results in an error. An WechatMPAuth.Error exception is raised if the request results in an error tuple ({:error, reason}).

request(method, token, url, body \\ "")

Specs

request(atom, t, binary, body) ::
  {:ok, Response.t} |
  {:error, Error.t}

Makes a request of given type to the given URL using the WechatMPAuth.ComponentAccessToken.

request!(method, token, url, body \\ "")

Specs

request!(atom, t, binary, body) ::
  Response.t |
  Error.t

Same as request/4 but returns WechatMPAuth.Response or raises an error if an error occurs during the request. An WechatMPAuth.Error exception is raised if the request results in an error tuple ({:error, reason}).