D
D
dreeeeedd2021-10-30 19:26:12
ruby
dreeeeedd, 2021-10-30 19:26:12

How to work with Spotify API via Ruby ( RestClient, Watir...)?

Hello,
I need to connect via ruby ​​and take a token from spotify (What I did),
but the token that they send me is low-level (from Flow'a client_credentials), and when I change Flow'a, I get a BAD REQUEST.

Token request:

grant = Base64.encode64("#{CLIENT_ID}:#{CLIENT_SECRET}").delete("\n")

respond = RestClient.post('https://accounts.spotify.com/api/token',
                       {'grant_type' => 'client_credentials'},
                       {"Authorization" => "Basic #{grant}"})

token_ext =  respond.split(',')
token_ext2 = token_ext[0].split(':')
only_token = token_ext2[1].delete_prefix('"').delete_suffix('"')
puts 'ACCES TOKEN: %s' % [only_token]


But the problem with the token is not everything, my main task is to work with the playlist.
Firstly, I need to add a new playlist, I tried to do this, but before that I did not work with HTTP Requests in this format.

My attempts to add a playlist (the code below doesn't work):
# respond = RestClient.post('https://api.spotify.com/v1/users/%s/playlists' % [USER_ID],
#                           {"Content-Type" => "application/json"},
#                           {"Authorization" => "Basic #{grant}"})
# respond = RestClient.post('https://api.spotify.com/v1/users/%s/playlists' % [USER_ID],
#
#                           {"Content-Type" => "application/json"},
#                           {"Authorization" => "Basic #{grant}"},
#                           ) { "name" => "New Playlilist" ; "description" => "sdada" ; "public" => "false"}


And so in the end I would like to return to the token, because at the end I was already tired of looking for ruby ​​guides on working with spotify, so I found the "spotify-client" gem. I threw the token into the config and tried to make a new playlist.

config = {
  :access_token => only_token.to_s,  # initialize the client with an access token to perform authenticated calls
  :raise_errors => true,  # choose between returning false or raising a proper exception when API calls fails

  # Connection properties
  :retries       => 1,    # automatically retry a certain number of times before returning
  :read_timeout  => 10,   # set longer read_timeout, default is 10 seconds
  :write_timeout => 10,   # set longer write_timeout, default is 10 seconds
  :persistent    => false # when true, make multiple requests calls using a single persistent connection. Use +close_connection+ method on the client to manually clean up sockets
}
client = Spotify::Client.new(config)
sleep(2)
puts client.create_user_playlist(USER_ID, "ALOHA", is_public = true)


As a result, I got an error that I do not have enough rights, that is, my token does not give me such rights.

Well, the question itself in the end. How can I get the right token (the right token from Flow'a authorization_code) from spotfi through a request using Watir or RestClient ? And also how do I add a new playlist and do such manipulations as adding a track to the playlist, changing the last track with the first one in the playlist and pulling out song data?

Thanks

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question