rnk
rnk

Reputation: 2204

HTTPError: 401 Client Error

I'm using Python soundcloud API to implement authenticated user's soundcloud videos in my web application. I followed this steps http://developers.soundcloud.com/docs#authentication, the first time I got everything working. I just tried those things once again from the first and now I'm getting HTTPError: 401 Client Error on this command current_user = client.get('/me')

I can show the steps I've done. Please check this https://gist.github.com/2945075

I'm getting this error:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/soundcloud-0.3-py2.7.egg/soundcloud/client.py", line 129, in _request
return wrapped_resource(make_request(method, url, kwargs))
File "/usr/local/lib/python2.7/dist-packages/soundcloud-0.3-py2.7.egg/soundcloud/request.py", line 180, in make_request
result.raise_for_status()
File "/usr/local/lib/python2.7/dist-packages/requests-0.10.1-py2.7.egg/requests/models.py", line 799, in raise_for_status
raise HTTPError('%s Client Error' % self.status_code)
HTTPError: 401 Client Error

How can I make these things to work? could anyone guide me? Thanks!

Upvotes: 0

Views: 10229

Answers (1)

Paul Osman
Paul Osman

Reputation: 4117

Your code looks correct. Just to sanity check, here's what I've done:

import soundcloud

client = soundcloud.Client(client_id='MY_CLIENT_ID',
                           client_secret='MY_CLIENT_SECRET',
                           redirect_uri='MY_REDIRECT_URI')
print client.authorize_url()

# visit authorization code in browser, grant access and copy and paste "code" param

code = 'MY_CODE'
access_token = client.exchange_token(code)

user = client.get('/me')
print user.username

# prints 'Paul Osman'

A few things to note that might be tripping you up:

  1. You don't have to recreate the client instance after calling exchange_token(). Doing so shouldn't hurt though.
  2. exchange_token() returns a Resource object with two properties (by default): access_token and scope.

Make sure when you're saving the access token that you're extracting the right property from the Resource object:

access_token = client.exchange_token('YOUR_CODE')
token = access_token.access_token

Another thing to try is to print out the full response from client.exchange_code:

access_token = client.exchange_token('YOUR_CODE')
print access_token.fields()

Hope that helps. Let me know if you're still experiencing problems and I'll edit my answer with more info.

Upvotes: 1

Related Questions