OAuth2.AccessToken
This module defines the OAuth2.AccessToken
struct and provides functionality
to make authorized requests to an OAuth2 provider using the AccessToken
returned by the provider.
The OAuth2.AccessToken
struct is created for you when you use the
OAuth2.Client.get_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
.- The headers from the
Client.headers
are appended to the request headers.
Examples
token = OAuth2.AccessToken.new("abc123", %OAuth2.Client{site: "www.example.com"})
case OAuth2.AccessToken.get(token, "/some/resource") do
{:ok, %OAuth2.Response{status_code: 401}} ->
"Not Good"
{:ok, %OAuth2.Response{status_code: status_code, body: body}} when status_code in [200..299] ->
"Yay!!"
{:error, %OAuth2.Error{reason: reason}} ->
reason
end
response = OAuth2.AccessToken.get!(token, "/some/resource")
response = OAuth2.AccessToken.post!(token, "/some/other/resources", %{foo: "bar"})
Summary
Functions
Makes a DELETE
request to the given URL using the OAuth2.AccessToken
Same as delete/5
but returns a OAuth2.Response
or OAuth2.Error
exception
if the request results in an error
Determines if the access token has expired
Determines if the access token will expire or not
Returns a unix timestamp based on now + expires_at (in seconds)
Makes a GET
request to the given url
using the OAuth2.AccessToken
struct
Same as get/4
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error
Same as new/2
except that the first arg is a map
Makes a PATCH
request to the given url
using the OAuth2.AccessToken
struct
Same as patch/5
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error
Makes a POST
request to the given URL using the OAuth2.AccessToken
Same as post/5
but returns a OAuth2.Response
or OAuth2.Error
exception
if the request results in an error
Makes a PUT
request to the given url
using the OAuth2.AccessToken
struct
Same as put/5
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error
Gets a new AccessToken
by making a request using the refresh_token
Calls refresh/3
but raises Error
if there an error occurs
Makes a request of given type to the given URL using the OAuth2.AccessToken
Same as request/6
but returns OAuth2.Response
or raises an error if an
error occurs during the request
Types
access_token :: binary
body :: binary | %{}
expires_at :: integer
other_params :: %{}
refresh_token :: binary
t :: %OAuth2.AccessToken{access_token: access_token, refresh_token: refresh_token, expires_at: expires_at, token_type: token_type, other_params: other_params, client: OAuth2.Client.t}
token_type :: binary
Functions
Specs
delete(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a DELETE
request to the given URL using the OAuth2.AccessToken
.
Specs
delete!(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as delete/5
but returns a OAuth2.Response
or OAuth2.Error
exception
if the request results in an error.
An OAuth2.Error
exception is raised if the request results in an
error tuple ({:error, reason}
).
Specs
expires?(OAuth2.AccessToken.t) :: boolean
Determines if the access token will expire or not.
Returns true
unless expires_at
is nil
.
Specs
get(t, binary, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a GET
request to the given url
using the OAuth2.AccessToken
struct.
Specs
get!(t, binary, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as get/4
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error.
Specs
new(binary, OAuth2.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 OAuth2.Client.get_token/4
to create the OAuth2.AccessToken
struct.
Example
iex(1)> OAuth2.AccessToken.new(%{"access_token" => "abc123"}, %OAuth2.Client{})
%OAuth2.AccessToken{access_token: "abc123",
client: %OAuth2.Client{authorize_url: "/oauth/authorize", client_id: "",
client_secret: "", headers: [], params: %{}, redirect_uri: "", site: "",
strategy: OAuth2.Strategy.AuthCode, token_method: :post,
token_url: "/oauth/token"}, expires_at: nil, other_params: %{},
refresh_token: nil, token_type: "Bearer"}
Specs
patch(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a PATCH
request to the given url
using the OAuth2.AccessToken
struct.
Specs
patch!(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as patch/5
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error.
An OAuth2.Error
exception is raised if the request results in an
error tuple ({:error, reason}
).
Specs
post(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a POST
request to the given URL using the OAuth2.AccessToken
.
Specs
post!(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as post/5
but returns a OAuth2.Response
or OAuth2.Error
exception
if the request results in an error.
An OAuth2.Error
exception is raised if the request results in an
error tuple ({:error, reason}
).
Specs
put(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a PUT
request to the given url
using the OAuth2.AccessToken
struct.
Specs
put!(t, binary, body, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as put/5
but returns a OAuth2.Response
or OAuth2.Error
exception if
the request results in an error.
An OAuth2.Error
exception is raised if the request results in an
error tuple ({:error, reason}
).
Specs
refresh(t, OAuth2.Client.params, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.AccessToken.t} |
{:error, OAuth2.Error.t}
Gets a new AccessToken
by making a request using the refresh_token
Returns an AccessToken
struct that can then be used to access the resource API.
Specs
refresh!(t, OAuth2.Client.params, OAuth2.Client.headers, Keyword.t) ::
OAuth2.AccessToken.t |
OAuth2.Error.t
Calls refresh/3
but raises Error
if there an error occurs.
Specs
request(atom, t, binary, body, OAuth2.Client.headers, Keyword.t) ::
{:ok, OAuth2.Response.t} |
{:error, OAuth2.Error.t}
Makes a request of given type to the given URL using the OAuth2.AccessToken
.
Specs
request!(atom, t, binary, body, OAuth2.Client.headers, Keyword.t) ::
OAuth2.Response.t |
OAuth2.Error.t
Same as request/6
but returns OAuth2.Response
or raises an error if an
error occurs during the request.
An OAuth2.Error
exception is raised if the request results in an
error tuple ({:error, reason}
).