OAuth2.Client

This module defines the OAuth2.Client struct and is responsible for building and establishing a request for an access token.

Summary

Functions

Returns the authorize url based on the client configuration

Initializes an OAuth2.AccessToken struct by making a request to the token endpoint

Same as get_token/4 but raises OAuth2.Error if an error occurs during the request

Set multiple params in the client in one call

Builds a new OAuth2 client struct using the opts provided

Adds a new header key if not present, otherwise replaces the previous value of that header with value

Set multiple headers in the client in one call

Puts the specified value in the params for the given key

Types

authorize_url :: binary
client_id :: binary
client_secret :: binary
headers :: [{binary, binary}]
param :: binary | %{binary => param} | [param]
params :: %{binary => param}
redirect_uri :: binary
site :: binary
strategy :: module
t :: %OAuth2.Client{strategy: strategy, client_id: client_id, client_secret: client_secret, site: site, authorize_url: authorize_url, token_url: token_url, token_method: token_method, params: params, headers: headers, redirect_uri: redirect_uri}
token_method :: :post | :get | atom
token_url :: binary

Functions

authorize_url!(client, params \\ [])

Specs

authorize_url!(t, list) :: binary

Returns the authorize url based on the client configuration.

Example

redirect_url = OAuth2.Client.authorize_url!(%OAuth2.Client{})
get_token(client, params \\ [], headers \\ [], opts \\ [])

Specs

get_token(t, params, headers, Keyword.t) ::
  {:ok, OAuth2.AccessToken.t} |
  {:error, OAuth2.Error.t}

Initializes an OAuth2.AccessToken struct by making a request to the token endpoint.

Returns an OAuth2.AccessToken struct that can then be used to access the provider’s RESTful API.

Arguments

  • client - a OAuth2.Client struct with the strategy to use, defaults to OAuth2.Strategy.AuthCode
  • params - a keyword list of request parameters
  • headers - a list of request headers
  • opts - a Keyword list of options

Options

  • :timeout - the timeout (in milliseconds) of the request
  • :proxy - a proxy to be used for the request; it can be a regular url or a {Host, Proxy} tuple
get_token!(client, params \\ [], headers \\ [], opts \\ [])

Same as get_token/4 but raises OAuth2.Error if an error occurs during the request.

merge_params(client, params)

Specs

merge_params(t, OAuth2.params) :: t

Set multiple params in the client in one call.

new(opts)

Specs

new(Keyword.t) :: t

Builds a new OAuth2 client struct using the opts provided.

Client struct fields

  • strategy - a module that implements the appropriate OAuth2 strategy, default OAuth2.Strategy.AuthCode
  • client_id - the client_id for the OAuth2 provider
  • client_secret - the client_secret for the OAuth2 provider
  • site - the OAuth2 provider site host
  • authorize_url - absolute or relative URL path to the authorization endpoint. Defaults to "/oauth/authorize"
  • token_url - absolute or relative URL path to the token endpoint. Defaults to "/oauth/token"
  • token_method - HTTP method to use to request token (:get or :post). Defaults to :post
  • params - a map of request parameters
  • headers - a list of request headers
  • redirect_uri - the URI the provider should redirect to after authorization or token requests
put_header(client, key, value)

Specs

put_header(t, binary, binary) :: t

Adds a new header key if not present, otherwise replaces the previous value of that header with value.

put_headers(client, list)

Specs

put_headers(t, list) :: t

Set multiple headers in the client in one call.

put_param(client, key, value)

Specs

put_param(t, String.t | atom, any) :: t

Puts the specified value in the params for the given key.

The key can be a string or an atom. Atoms are automatically convert to strings.