class Spotify::Accounts

Spotify::Accounts deals with authorization using the Spotify Accounts API.

Constants

SCOPES

An entire list of Spotify's OAuth scopes. Stored in the form of a symbolized array. Example: `[:scope1, :scope2]`

@see developer.spotify.com/documentation/general/guides/scopes/

Last updated: 23 June 2018

Attributes

client_id[RW]
client_secret[RW]
redirect_uri[RW]

Public Class Methods

new(config={}) click to toggle source

Initialize the Spotify Accounts object.

@example

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@accounts = Spotify::Accounts.new
@accounts.client_id = "[client id goes here]"
@accounts.client_secret = "[client secret goes here]"
@accounts.redirect_uri = "http://localhost"

# with SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, and SPOTIFY_REDIRECT_URI in ENV:
@accounts = Spotify::Accounts.new

@param [Hash] config The configuration containing your Client ID, Client Secret, and your Redirect URL.

@see developer.spotify.com/dashboard/

# File lib/spotify/accounts.rb, line 61
def initialize(config={})
  @client_id = config.delete(:client_id) { ENV["SPOTIFY_CLIENT_ID"] }
  @client_secret = config.delete(:client_secret) { ENV["SPOTIFY_CLIENT_SECRET"] }
  @redirect_uri = config.delete(:redirect_uri) { ENV["SPOTIFY_REDIRECT_URI"] }
end

Public Instance Methods

authorize_url(override_params={}) click to toggle source

Get a HTTP URL to send user for authorizing with Spotify.

@example

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@auth.authorize_url
@auth.authorize_url({ scope: "user-read-private user-top-read" })

@param [Hash] override_params Optional hash containing any overriding values for parameters. Parameters used are client_id, redirect_uri, response_type and scope. @return [String] A fully qualified Spotify authorization URL to send the user to.

@see developer.spotify.com/documentation/general/guides/authorization-guide/

# File lib/spotify/accounts.rb, line 88
def authorize_url(override_params={})
  validate_credentials!
  params = {
    client_id:     @client_id,
    redirect_uri:  @redirect_uri,
    response_type: "code",
    scope:         SCOPES.join(" ")
  }.merge(override_params)
  "https://accounts.spotify.com/authorize?%s" % params.to_query
end
exchange_for_session(code) click to toggle source

Start a session from your authentication code.

@example

@accounts = Spotify::Accounts.new({
  client_id: "[client id goes here]",
  client_secret: "[client secret goes here]",
  redirect_uri: "http://localhost"
})

@accounts.exchange_for_session("code")

@param [String] code The code provided back to your application upon authorization. @return [Spotify::Accounts::Session] session The session object.

@see developer.spotify.com/documentation/general/guides/authorization-guide/

# File lib/spotify/accounts.rb, line 116
def exchange_for_session(code)
  validate_credentials!
  Spotify::Accounts::Session.from_authorization_code(code)
end