buntine / discogs

A Ruby wrapper of the Discogs.com API
Other
220 stars 47 forks source link

'raise_unknown_resource exception' if try to get user folders being authentificated as the user #42

Closed v-vylinsky closed 9 years ago

v-vylinsky commented 9 years ago

If run the code pulling my collection folders,(my_app_key, my_secret_key are keys for my registered app):

require 'discogs-wrapper'
require 'hashie'
wrapper = Discogs::Wrapper.new(App_name, app_key: my_app_key, app_secret: my_secret_key)

puts wrapper.get_user_folders(my_username)

it ends up with error like this, looks like 404 response returned:

C:/Ruby22/lib/ruby/gems/2.2.0/gems/discogs-wrapper-2.1.1/lib/wrapper/wrapper.rb:802:in 'raise_unknown_resource': Unknown Discogs resource: users/xadnight/collection/folders (Discogs::UnknownResource)
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/discogs-wrapper-2.1.1/lib/wrapper/wrapper.rb:733:in 'query_api'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/discogs-wrapper-2.1.1/lib/wrapper/wrapper.rb:723:in 'query_and_build'
    from C:/Ruby22/lib/ruby/gems/2.2.0/gems/discogs-wrapper-2.1.1/lib/wrapper/wrapper.rb:376:in 'get_user_folders'
    from C:/Users/Uladzimir/RubymineProjects/untitled/discogs_client.rb:20:in '<class:DiscogsClient>'
    from C:/Users/Uladzimir/RubymineProjects/untitled/discogs_client.rb:5:in '<top (required)>'

however if remove the authentication params when initiate wrapper with no keys, like this: wrapper = Discogs::Wrapper.new(App_name)

the same code returns the public 'All' folder:

#<Hashie::Mash folders=[#<Hashie::Mash count=316 id=0 name="All" resource_url="http://api.discogs.com/users/xadnight/collection/folders/0">]>

I expected to get the list of all my folder, including private ones since I had send requests being authenticated.

ps. I'm new to Ruby, so please don't blame me so much. Thanks!

buntine commented 9 years ago

Hey mate,

Thanks for this. It looks like Discogs have changed their self-authentication process. You now need to generate a "user token" from your API dashboard. The official docs still talk about the "key" and "secret", though.

I'll update my library and release a new version of the gem asap.

I cannot replicate your issue with the "Unrecognised Discogs Resource", though.

cheers, Andrew.

buntine commented 9 years ago

Ok, I've just pushed a new version of the Gem and updated the README.

Update to version 2.1.4 (bundle update or gem install) and change your code to suit the new example. Let me know how you go!

buntine commented 9 years ago

Assumed fixed, closing now.

v-vylinsky commented 9 years ago

Thank you for the response! My apologies for not posting in this thread for quite a while - there were quite busy days for me. Anyway, I've checked the authentification in the 2.1.4 finally and this is what I found.

In the case of authentication using app keys (like in the code I had posted initially), it returned the public folder only

#<Hashie::Mash folders=[#<Hashie::Mash count=316 id=0 name="All" resource_url="https://api.discogs.com/users/xadnight/collection/folders/0">]>

However, if authenticate using the user_token:

wrapper = Discogs::Wrapper.new(App_name, user_token: my_token)

it returns all my private folders along with the public one.

It looks like registered app in Discogs isn't associated with the owner account, so that being authenticated with app keys doesn't grant access to the app owners private data, like folders.

Anyway, the described issue is solved, the authentication works now and doesn't return the error like it used to. Thank you a lot!

buntine commented 9 years ago

Thanks, no problems. I will take a look soon.

When you authenticate with oauth, you are following through the whole redirect process, right?

v-vylinsky commented 9 years ago

Uhm, nope, just like this:

wrapper = Discogs::Wrapper.new(App_name, app_key: my_app_key, app_secret: my_secret_key)

puts wrapper.get_user_folders(my_username)

The app I'm building isn't user-faced, it's just a script fetching my collection data into a CSV, so I decided to keep it simple, and I skipped the whole oauth dance :). Or you mean this is not the way it supposed to work, right?

buntine commented 9 years ago

Ok, so the app_key and app_secret way won't work for self-authentication anymore. It was a temporary solution on the Discogs end that they have replaced with user_token. I've actually completely removed app_key and app_secret options from the latest version of the gem!

You have two options:

1) Use your own user_token directly (to access your own stuff) 2) Follow through the "dance" in order to let your users access their own info

Because you mentioned that you only ever want to access your own stuff (and your not doing it within a web browser, I guess?), you must use the first approach.

Makes sense?

v-vylinsky commented 9 years ago

Yes, this makes sense, thanks a lot! Your guess is correct, the app is just a console script for now, not a browser app whatsoever.

buntine commented 9 years ago

Ok, cool. Glad to help. :)