View Source TwitchAPI.Auth (hello_twitch_api v0.4.4)

Twitch API Auth (and struct).

Functions for managing your auth and tokens.

Summary

Functions

Merge string params into Auth struct.

Make a new Auth struct with a client_id.

Make a new Auth struct with a client_id and client_secret.

Make a new Auth struct with a client_id, client_secret, and access_token.

Add an access_token to the Auth struct.

Add a client_secret to the Auth struct.

Get an access token with an authorization code.

Refresh an access token.

Revoke an access token.

Validate an access token.

Types

@type t() :: %TwitchAPI.Auth{
  access_token: String.t() | nil,
  client_id: String.t(),
  client_secret: String.t() | nil,
  expires_at: DateTime.t() | nil,
  refresh_token: String.t() | nil
}

Functions

Link to this function

merge_string_params(auth, params)

View Source
@spec merge_string_params(t(), params :: %{required(String.t()) => term()}) :: t()

Merge string params into Auth struct.

Example

iex> auth = Auth.new("some-client-id")
iex> params = %{"access_token" => "abc123", "refresh_token" => "def456"}
iex> Auth.merge_string_params(auth, params)
%Auth{
  client_id: "some-client-id",
  access_token: "abc123",
  refresh_token: "def456"
}
@spec new(client_id :: String.t()) :: t()

Make a new Auth struct with a client_id.

Example

iex> Auth.new("some-client-id")
%Auth{client_id: "some-client-id"}
Link to this function

new(client_id, client_secret)

View Source
@spec new(client_id :: String.t(), client_secret :: String.t()) :: t()

Make a new Auth struct with a client_id and client_secret.

Example

iex> Auth.new("some-client-id", "secretssss")
%Auth{client_id: "some-client-id", client_secret: "secretssss"}
Link to this function

new(client_id, client_secret, access_token)

View Source
@spec new(
  client_id :: String.t(),
  client_secret :: String.t(),
  access_token :: String.t()
) :: t()

Make a new Auth struct with a client_id, client_secret, and access_token.

Example

iex> Auth.new("some-client-id", "secretssss", "sometokenabc123")
%Auth{client_id: "some-client-id", client_secret: "secretssss", access_token: "sometokenabc123"}
Link to this function

put_access_token(auth, access_token)

View Source
@spec put_access_token(t(), access_token :: String.t()) :: t()

Add an access_token to the Auth struct.

Example

iex> auth = Auth.new("some-client-id")
iex> Auth.put_access_token(auth, "abc123")
%Auth{client_id: "some-client-id", access_token: "abc123"}
Link to this function

put_client_secret(auth, client_secret)

View Source
@spec put_client_secret(t(), client_secret :: String.t()) :: t()

Add a client_secret to the Auth struct.

Example

iex> auth = Auth.new("some-client-id")
iex> Auth.put_client_secret(auth, "secretssss")
%Auth{client_id: "some-client-id", client_secret: "secretssss"}
Link to this function

token_get_from_code(auth, code, redirect_url)

View Source
@spec token_get_from_code(t(), code :: String.t(), redirect_url :: String.t()) ::
  {:ok, Req.Response.t()} | {:error, term()}

Get an access token with an authorization code.

https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#authorization-code-grant-flow

If the request succeeds, it returns an access token and refresh token.

{
  "access_token": "rfx2uswqe8l4g1mkagrvg5tv0ks3",
  "expires_in": 14124,
  "refresh_token": "5b93chm6hdve3mycz05zfzatkfdenfspp1h1ar2xxdalen01",
  "scope": [
    "channel:moderate",
    "chat:edit",
    "chat:read"
  ],
  "token_type": "bearer"
}
@spec token_refresh(t()) :: {:ok, Req.Response.t()} | {:error, term()}

Refresh an access token.

https://dev.twitch.tv/docs/authentication/refresh-tokens

If the request succeeds, the response contains the new access token, refresh token, and scopes associated with the new grant. Because refresh tokens may change, your app should safely store the new refresh token to use the next time.

{
  "access_token": "1ssjqsqfy6bads1ws7m03gras79zfr",
  "refresh_token": "eyJfMzUtNDU0OC4MWYwLTQ5MDY5ODY4NGNlMSJ9%asdfasdf=",
  "scope": [
    "channel:read:subscriptions",
    "channel:manage:polls"
  ],
  "token_type": "bearer"
}

The following example shows what the response looks like if the request fails.

{
  "error": "Bad Request",
  "status": 400,
  "message": "Invalid refresh token"
}
@spec token_revoke(t()) :: {:ok, Req.Response.t()} | {:error, term()}

Revoke an access token.

https://dev.twitch.tv/docs/authentication/revoke-tokens

If the revocation succeeds, the request returns HTTP status code 200 OK (with no body).

If the revocation fails, the request returns one of the following HTTP status codes:

  • 400 Bad Request if the client ID is valid but the access token is not.

    { "status": 400, "message": "Invalid token" }

  • 404 Not Found if the client ID is not valid.

    { "status": 404, "message": "client does not exist" }

@spec token_validate(t()) :: {:ok, Req.Response.t()} | {:error, term()}

Validate an access token.

https://dev.twitch.tv/docs/authentication/validate-tokens/#how-to-validate-a-token

If the token is valid, the request returns HTTP status code 200 and the response’s body contains the following JSON object:

{
  "client_id": "wbmytr93xzw8zbg0p1izqyzzc5mbiz",
  "login": "twitchdev",
  "scopes": [
    "channel:read:subscriptions"
  ],
  "user_id": "141981764",
  "expires_in": 5520838
}

If the token is not valid, the request returns HTTP status code 401 and the response’s body contains the following JSON object:

{
  "status": 401,
  "message": "invalid access token"
}