goth v0.4.0 Goth.Token

Interface for retrieving access tokens, from either the Goth.TokenStore or the Google token API. The first request for a token will hit the API, but subsequent requests will retrieve the token from Goth’s token store.

Goth will automatically refresh access tokens in the background as necessary, 10 seconds before they are to expire. After the initial synchronous request to retrieve an access token, your application should never have to wait for a token again.

The first call to retrieve an access token for a particular scope blocks while it hits the API. Subsequent calls pull from the Goth.TokenStore, and should return immediately

iex> Goth.Token.for_scope("https://www.googleapis.com/auth/pubsub")
{:ok, %Goth.Token{token: "23984723",
                  type: "Bearer",
                  scope: "https://www.googleapis.com/auth/pubsub",
                  expires: 1453653825}}

For using the token on subsequent requests to the Google API, just concatenate the type and token to create the authorization header. An example using HTTPoison:

{:ok, token} = Goth.Token.for_scope("https://www.googleapis.com/auth/pubsub")
HTTPoison.get(url, [{"Authorization", "#{token.type} #{token.token}"}])

Summary

Functions

Get a %Goth.Token{} for a particular scope. scope can be a single scope or multiple scopes joined by a space

Parse a successful JSON response from Google’s token API and extract a %Goth.Token{}

Retrieve a new access token from the API. This is useful for expired tokens, although Goth automatically handles refreshing tokens for you, so you should rarely if ever actually need to call this method manually

Types

t :: %Goth.Token{expires: non_neg_integer, scope: String.t, token: String.t, type: String.t}

Functions

for_scope(scope)

Specs

for_scope(String.t) :: {:ok, t} | :error

Get a %Goth.Token{} for a particular scope. scope can be a single scope or multiple scopes joined by a space.

Example

iex> Token.for_scope("https://www.googleapis.com/auth/pubsub")
{:ok, %Goth.Token{expires: ..., token: "...", type: "..."} }
from_response_json(scope, json)

Specs

from_response_json(String.t, String.t) :: t

Parse a successful JSON response from Google’s token API and extract a %Goth.Token{}

queue_for_refresh(token)
refresh!(scope)

Specs

refresh!(t | String.t) :: {:ok, t}

Retrieve a new access token from the API. This is useful for expired tokens, although Goth automatically handles refreshing tokens for you, so you should rarely if ever actually need to call this method manually.