Current section
Files
Jump to
Current section
Files
lib/spotifex/oauth_strategy.ex
defmodule Spotifex.OAuthStrategy do
use OAuth2.Strategy
alias OAuth2.Client
alias OAuth2.Strategy.AuthCode
alias Spotifex.Config
@scopes ~w(playlist-read-private playlist-read-collaborative
playlist-modify-public playlist-modify-private user-follow-modify
user-library-read user-library-modify user-read-private
user-read-birthdate user-read-email user-top-read)
# Public API
def new do
config = Config.get
Client.new([
strategy: __MODULE__,
client_id: config.client_id,
client_secret: config.client_secret,
redirect_uri: config.redirect_uri,
site: "https://api.spotify.com/",
authorize_url: "https://accounts.spotify.com/authorize/",
token_url: "https://accounts.spotify.com/api/token"
])
end
def authorize_url!(scope, state) do
scopes = scope
|> Enum.map(fn s -> to_string(s) end)
|> Enum.filter(fn s -> Enum.member?(@scopes, s) end)
|> Enum.join(" ")
new()
|> put_param(:scope, scopes)
|> put_param(:state, state)
|> Client.authorize_url!([])
end
def authorize_url!(scope) do
scopes = scope
|> Enum.map(fn s -> to_string(s) end)
|> Enum.filter(fn s -> Enum.member?(@scopes, s) end)
|> Enum.join(" ")
new()
|> put_param(:scope, scopes)
|> Client.authorize_url!([])
end
def authorize_url! do
new()
|> Client.authorize_url!([])
end
# you can pass options to the underlying http library via `options` parameter
def get_token!(params \\ [], headers \\ [], options \\ []) do
Client.get_token!(new(), params, headers, options)
end
# Strategy Callbacks
def authorize_url(client, params) do
AuthCode.authorize_url(client, params)
end
def get_token(client, params, headers) do
client
|> put_header("Accept", "application/json")
|> AuthCode.get_token(params, headers)
end
end