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.

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:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/spotify/auth.rb', line 50

def initialize(config)
  validate_initialized_input(config)
  client_id = config.delete(:client_id)
  client_secret = config.delete(:client_secret)
  opts = {
    site:          "https://api.spotify.com",
    authorize_url: "https://accounts.spotify.com/oauth/authorize",
    token_url:     "https://accounts.spotify.com/api/token"
  }.merge(config)
  super(client_id, client_secret, opts)
end

Instance Method Details

#authorize_url(override_params = {}) ⇒ String

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.

Returns:

  • (String)

    A fully qualified Spotify authorization URL to send the user to.

See Also:



80
81
82
83
84
85
86
# File 'lib/spotify/auth.rb', line 80

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