Class: Rockstar::TokenAuth
- Inherits:
-
Object
- Object
- Rockstar::TokenAuth
- Defined in:
- lib/rockstar/tokenauth.rb
Overview
Token Authentification
There are 2 ways to get an auth token :
Desktop-App
-
Get a new token to request authorisation:
token = Rockstar::Auth.new.token
-
Open a webbrowser with www.last.fm/api/auth/?api_key=xxxxxxxxxxx&token=xxxxxxxx
-
Wait for the User to confirm that he accepted your request.
-
Continue with “Get the session token”
Web-App
-
Redirect the user to www.last.fm/api/auth/?api_key=YOUR_API_KEY&cb=YOUR_RETURN_URL
-
If the user accepts, lastfm will redirect to YOUR_RETURN_URL?token=TOKEN
token = params[:token]
-
Continue with “Get the session token”
Get the session token
-
Use the previous token and call
session = Rockstar::Auth.new.session(token)
-
Store the session.key and session.username returned. The session.key will not expire. It is save to store it into your database.
-
Use this session.key as token to authentificate with this class :
auth = Rockstar::TokenAuth.new({:username => 'chunky', :token => 'bacon'}) auth.handshake!
Instance Attribute Summary collapse
-
#client_id ⇒ Object
you should read last.fm/api/submissions#handshake.
-
#client_ver ⇒ Object
you should read last.fm/api/submissions#handshake.
-
#now_playing_url ⇒ Object
readonly
Returns the value of attribute now_playing_url.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#submission_url ⇒ Object
readonly
Returns the value of attribute submission_url.
-
#token ⇒ Object
you should read last.fm/api/submissions#handshake.
-
#user ⇒ Object
you should read last.fm/api/submissions#handshake.
Instance Method Summary collapse
- #handshake! ⇒ Object
-
#initialize(args = {}) ⇒ TokenAuth
constructor
A new instance of TokenAuth.
Constructor Details
#initialize(args = {}) ⇒ TokenAuth
Returns a new instance of TokenAuth.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rockstar/tokenauth.rb', line 41 def initialize(args = {}) @user = args[:username] # last.fm user @token = args[:token] # last.fm token @client_id = 'rck' # Client ID assigned by last.fm; Don't change this! @client_ver = Rockstar::Version raise ArgumentError, 'Missing required argument' if @user.blank? || @token.blank? @connection = REST::Connection.new(Rockstar::AUTH_URL) end |
Instance Attribute Details
#client_id ⇒ Object
you should read last.fm/api/submissions#handshake
38 39 40 |
# File 'lib/rockstar/tokenauth.rb', line 38 def client_id @client_id end |
#client_ver ⇒ Object
you should read last.fm/api/submissions#handshake
38 39 40 |
# File 'lib/rockstar/tokenauth.rb', line 38 def client_ver @client_ver end |
#now_playing_url ⇒ Object (readonly)
Returns the value of attribute now_playing_url.
39 40 41 |
# File 'lib/rockstar/tokenauth.rb', line 39 def @now_playing_url end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id.
39 40 41 |
# File 'lib/rockstar/tokenauth.rb', line 39 def session_id @session_id end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
39 40 41 |
# File 'lib/rockstar/tokenauth.rb', line 39 def status @status end |
#submission_url ⇒ Object (readonly)
Returns the value of attribute submission_url.
39 40 41 |
# File 'lib/rockstar/tokenauth.rb', line 39 def submission_url @submission_url end |
#token ⇒ Object
you should read last.fm/api/submissions#handshake
38 39 40 |
# File 'lib/rockstar/tokenauth.rb', line 38 def token @token end |
#user ⇒ Object
you should read last.fm/api/submissions#handshake
38 39 40 |
# File 'lib/rockstar/tokenauth.rb', line 38 def user @user end |
Instance Method Details
#handshake! ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rockstar/tokenauth.rb', line 52 def handshake! = Time.now.to_i.to_s auth = Digest::MD5.hexdigest("#{Rockstar.lastfm_api_secret}#{}") query = { :hs => 'true', :p => AUTH_VER, :c => @client_id, :v => @client_ver, :u => @user, :t => , :a => auth, :api_key=>Rockstar.lastfm_api_key, :sk => @token } result = @connection.get('/', true, query) @status = result.split(/\n/)[0] case @status when /OK/ @session_id, @now_playing_url, @submission_url = result.split(/\n/)[1,3] when /BANNED/ raise BannedError # something is wrong with the gem, check for an update when /BADAUTH/ raise BadAuthError # invalid user/password when /FAILED/ raise RequestFailedError, @status when /BADTIME/ raise BadTimeError # system time is way off else raise RequestFailedError end end |