我正在使用Python soundcloud API在我的Web应用程序中实现经过身份验证的用户的soundcloud视频。我按照这个步骤进行http://developers.soundcloud.com/docs#authentication,这是我第一次完成所有工作。我刚刚从第一次尝试了这些事情,现在我在这个命令current_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
如何让这些东西发挥作用?谁能指导我?谢谢!
答案 0 :(得分: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'
要注意的一些事情可能会让你失望:
exchange_token()
后,您不必重新创建客户端实例。这样做不应该伤害。 exchange_token()
返回一个具有两个属性的Resource对象(默认情况下):access_token
和scope
。确保在保存访问令牌时从Resource对象中提取正确的属性:
access_token = client.exchange_token('YOUR_CODE')
token = access_token.access_token
要尝试的另一件事是打印出来自client.exchange_code的完整响应:
access_token = client.exchange_token('YOUR_CODE')
print access_token.fields()
希望有所帮助。如果您仍然遇到问题,请告诉我,我会用更多信息编辑我的答案。