guilhermesad / rspotify

A ruby wrapper for the Spotify Web API
MIT License
717 stars 290 forks source link

Update documentation to include examples of initialisation #218

Open dylan-hoefsloot opened 4 years ago

dylan-hoefsloot commented 4 years ago

The only information I could find in the documentation, if you want to create a new user without adding RSpotify as middleware in rails was this:

If you'd like to use OAuth outside rails, have a look here for the requests that need to be made. You should be able to pass the response to RSpotify::User.new just as well, and from there easily create playlists and more for your user

However, it is a bit unclear.

You should be able to pass the response to RSpotify::User.new just as well.

Which response? It is not specified. I thought it would be safe to assume it takes the response from Spotify containing

auth_params = {
  access_token 
  token_type 
  scope
  expires_in
  refresh_token
}

However this didn't work, so I looked at the source for User and it is looking for a credentials field, which Spotify does not return in any of the flows.

I tried passing the above auth_params to the new method like so User.new('credentials' => auth_params), while this stopped me getting error about credentials not existing, all the internal RSpotify requests were returning 400 Bad Request

For these reasons I think it is worth adding an example of what RSpotify::User.new expects.

I think it is also worth adding something about having to execute RSpotify.authenticate('CLIENT_ID', 'CLIENT_SECRET') before making non user specific requests, as I only found this through looking at the Rails OAuth example, and you need it regardless of OAuth.

I still haven't figured this out, but if someone can give me an example I would be more than happy to update the documentation.

robbyrussell commented 4 years ago

@dylan-hoefsloot did you get anywhere with this? I'm trying to build a CLI tool to rebuild a playlist on demand (for my own user account). Hitting this error:

irb(main):012:0> spotify_user.create_playlist!('meep123')
Traceback (most recent call last):
        8: from /Users/robbyrussell/.rbenv/versions/2.7.1/bin/irb:23:in `<main>'
        7: from /Users/robbyrussell/.rbenv/versions/2.7.1/bin/irb:23:in `load'
        6: from /Users/robbyrussell/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/irb-1.2.3/exe/irb:11:in `<top (required)>'
        5: from (irb):12
        4: from /Users/robbyrussell/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rspotify-2.9.1/lib/rspotify/user.rb:129:in `create_playlist!'
        3: from /Users/robbyrussell/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rspotify-2.9.1/lib/rspotify/user.rb:77:in `block (2 levels) in <class:User>'
        2: from /Users/robbyrussell/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rspotify-2.9.1/lib/rspotify/user.rb:63:in `oauth_send'
        1: from /Users/robbyrussell/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/rspotify-2.9.1/lib/rspotify/user.rb:55:in `oauth_header'
NameError (uninitialized class variable @@users_credentials in RSpotify::User)
ducmtran commented 3 years ago

@robbyrussell i can't believe i ran into the creator of oh-my-zsh here, i never look you up but i instantly recognize your name from the default theme. I have used oh-my-zsh since I started coding and have always been a fan

for the issue you are having, I don't think you can create CLI app that modifies user data (playlist). Anything the modifies user data need to be redirected and confirmed by user. I think they mentioned that in the doc in FAQ at the bottom https://developer.spotify.com/documentation/general/guides/authorization-guide/ but you might have already found this :smile:

that said i'm having issues with logging in with oauth with the gem as well