Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Created May 30, 2024 22:13
Show Gist options
  • Save YannickFricke/1350c9ac3f8699e91ea25ee529a1c917 to your computer and use it in GitHub Desktop.
Save YannickFricke/1350c9ac3f8699e91ea25ee529a1c917 to your computer and use it in GitHub Desktop.
Twitch build authorization URL
@doc """
Builds an authorization URL which should be visited by users in order to connect Twitch to your application.
Reference: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#implicit-grant-flow
"""
@spec build_authorization_url(
client_id :: String.t(),
redirect_uri :: String.t(),
scopes :: list(Scope.t())
) :: String.t()
@spec build_authorization_url(
client_id :: String.t(),
redirect_uri :: String.t(),
scopes :: list(Scope.t()),
opts :: Keyword.t()
) :: String.t()
def build_authorization_url(client_id, redirect_uri, scopes, opts \\ []) do
response_type = Keyword.get(opts, :response_type, :code)
state = Keyword.get(opts, :state)
force_verify = Keyword.get(opts, :force_verify, false)
query_params = %{
"response_type" => response_type,
"client_id" => client_id,
"redirect_uri" => redirect_uri,
"scope" => Enum.join(scopes, " "),
"force_verify" => force_verify
}
query_params_with_state =
if state != nil do
Map.put(query_params, "state", state)
else
query_params
end
"https://id.twitch.tv/oauth2/authorize"
|> URI.new!()
|> URI.append_query(URI.encode_query(query_params_with_state))
|> URI.to_string()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment