0

我正在使用 Python soundcloud API 在我的 Web 应用程序中实现经过身份验证的用户的 soundcloud 视频。我遵循了这些步骤http://developers.soundcloud.com/docs#authentication,这是我第一次让一切正常。我刚从一开始就再次尝试了这些事情,现在我收到了HTTPError: 401 Client Error on this commandcurrent_user = client.get('/me')

我可以展示我已经完成的步骤。请检查此https://gist.github.com/2945075

我收到此错误:

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

我怎样才能让这些东西发挥作用?谁能指导我?谢谢!

4

1 回答 1

1

您的代码看起来正确。只是为了健全性检查,这就是我所做的:

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'

需要注意的一些事情可能会让你绊倒:

  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.

于 2012-06-17T22:09:39.557 回答