Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a small OAuth bug #99

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/rspotify/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def send_request(verb, path, *params)
rescue RestClient::Unauthorized
if @client_token
authenticate(@client_id, @client_secret)
params[-1]['Authorization'] = "Bearer #{@client_token}"
response = RestClient.send(verb, url, *params)
end
end
Expand Down
9 changes: 5 additions & 4 deletions lib/rspotify/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ def self.refresh_token(user_id)
}
response = RestClient.post(TOKEN_URI, request_body, RSpotify.send(:auth_header))
response = JSON.parse(response)
@@users_credentials[user_id]['token'] = response['access_token']
@@users_credentials[user_id]['access_token'] = response['access_token']
end
private_class_method :refresh_token

def self.oauth_header(user_id)
{
'Authorization' => "Bearer #{@@users_credentials[user_id]['token']}",
'Authorization' => "Bearer #{@@users_credentials[user_id]['access_token']}",
'Content-Type' => 'application/json'
}
end
private_class_method :oauth_header

def self.oauth_send(user_id, verb, path, *params)
RSpotify.send(:send_request, verb, path, *params)
rescue RestClient::Unauthorized => e
raise e if e.response !~ /access token expired/
rescue => e
refresh_token(user_id)
params[-1] = oauth_header(user_id)
RSpotify.send(:send_request, verb, path, *params)
Expand Down Expand Up @@ -85,6 +84,8 @@ def initialize(options = {})
@@users_credentials[@id] = credentials
@credentials = @@users_credentials[@id]
end

raise "Error - User ID cannot be empty." if @id.nil? || @id.empty?
end

# Creates a playlist in user's Spotify account. This method is only available when the current
Expand Down