Class: Spotify::Auth

Inherits:
OAuth2::Client
  • Object
show all
Defined in:
lib/spotify/auth.rb

Overview

Spotify::Auth inherits from OAuth2::Client based on the “oauth-2” gem.

Constant Summary collapse

SCOPES =

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

Last updated: 21 October 2017

%i[
  playlist-read-private playlist-read-collaborative
  playlist-modify-public playlist-modify-private
  ugc-image-upload user-follow-modify user-follow-read
  user-library-read user-library-modify user-read-private
  user-read-birthdate user-read-email user-top-read
  user-read-playback-state user-modify-playback-state
  user-read-currently-playing user-read-recently-played
  streaming
].freeze
OAUTH_I18N =

Error-related translations we’re using.

{
  must_be_hash: "Must be a Hash. Example: Spotify::Auth.new({ client_id: '', ... })",
  require_attr: "Expecting a '%s' key. You can obtain from https://developer.spotify.com"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Auth

Initialize the Spotify Auth object.

The redirect URL can be overriden later.

Examples:


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

Parameters:

  • config (Hash)

    OAuth configuration containing the Client ID, secret and redirect URL.

See Also:



54
55
56
57
58
59
60
61
62
# File 'lib/spotify/auth.rb', line 54

def initialize(config)
  opts = {
    site:          "https://api.spotify.com",
    authorize_url: "https://accounts.spotify.com/oauth/authorize"
  }
  validate_initialized_input(config)
  @redirect_uri = config[:redirect_uri]
  super(config[:client_id], config[:client_secret], opts)
end

Instance Method Details

#authorize_url(override_params = {}) ⇒ Object

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

Parameters used are client_id, redirect_uri, response_type and scope.

Examples:


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

@auth.authorize_url

Parameters:

  • override_params (Hash) (defaults to: {})

    Optional hash containing any overriding values for parameters.

See Also:



82
83
84
85
86
87
88
89
# File 'lib/spotify/auth.rb', line 82

def authorize_url(override_params={})
  super({
    client_id:     id,
    redirect_uri:  redirect_uri,
    response_type: "code",
    scope:         SCOPES.join(" ")
  }.merge(override_params))
end