[英]Soundcloud API - authentication works via curl, but not via Python script?
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.我可以使用 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)
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. client_id
和client_secret
是正确的,因为我有一个执行查询的脚本——只要我有访问令牌,它就可以正常运行。
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?
我只能通过 curl 获取访问令牌吗?
edit: This is the curl command that I use to get an access token编辑:这是我用来获取访问令牌的 curl 命令
https://developers.soundcloud.com/docs/api/guide#client-creds 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"
It's a post request, so I needed to post a "data" object in the body, not a "header".这是一个发布请求,所以我需要在正文中发布一个“数据”object,而不是“标题”。 Silly mistake
愚蠢的错误
def refresh_access_token(self):
data = {
"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, data=data)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.