Soundcloud API - 身份验证可以通过curl 进行,但不能通过Python 脚本进行?
我可以使用curl 成功进行身份验证并获取访问+刷新令牌,但我的Python 身份验证脚本无法工作并返回401 错误。
authentication_endpoint = 'https://api.soundcloud.com/oauth2/token'
def refresh_access_token(self):
headers = {
"accept": 'application/json; charset=utf-8',
"Content-Type": 'application/x-www-form-urlencoded',
"grant_type": 'client_credentials',
"client_id": self.client_id,
"client_secret": self.client_secret,
}
req = requests.post(self.authentication_endpoint, headers=headers)
client_id
和 client_secret
是正确的,因为我有一个执行查询的脚本——只要我有访问令牌,它就可以正常运行。
由于查询脚本运行没有问题,我不确定身份验证脚本出了什么问题。我只能通过curl 获取访问令牌吗?
编辑:这是我用来获取访问令牌的curl命令
https:// /developers.soundcloud.com/docs/api/guide#client-creds
# obtain the access token
$ curl -X POST "https://api.soundcloud.com/oauth2/token" \
-H "accept: application/json; charset=utf-8" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=YOUR_CLIENT_ID" \
--data-urlencode "client_secret=YOUR_CLIENT_SECRET"
I can successfully authenticate and get an access + refresh token using curl, but my Python authentication script won't work and returns a 401 error.
authentication_endpoint = 'https://api.soundcloud.com/oauth2/token'
def refresh_access_token(self):
headers = {
"accept": 'application/json; charset=utf-8',
"Content-Type": 'application/x-www-form-urlencoded',
"grant_type": 'client_credentials',
"client_id": self.client_id,
"client_secret": self.client_secret,
}
req = requests.post(self.authentication_endpoint, headers=headers)
The client_id
and client_secret
are correct because I have a script that performs queries -- which runs just fine as long as I have an access token.
Since the query script runs without problems, I'm not sure what's wrong with the authentication script. Am I only able to get the access token via curl?
edit: This is the curl command that I use to get an access token
https://developers.soundcloud.com/docs/api/guide#client-creds
# obtain the access token
$ curl -X POST "https://api.soundcloud.com/oauth2/token" \
-H "accept: application/json; charset=utf-8" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=YOUR_CLIENT_ID" \
--data-urlencode "client_secret=YOUR_CLIENT_SECRET"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个发布请求,所以我需要在正文中发布一个“数据”对象,而不是“标题”。愚蠢的错误
It's a post request, so I needed to post a "data" object in the body, not a "header". Silly mistake