Soundcloud API - 身份验证可以通过curl 进行,但不能通过Python 脚本进行?

发布于 2025-01-10 21:11:32 字数 1271 浏览 0 评论 0原文

我可以使用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_idclient_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摘星┃星的人 2025-01-17 21:11:32

这是一个发布请求,所以我需要在正文中发布一个“数据”对象,而不是“标题”。愚蠢的错误

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)

It's a post request, so I needed to post a "data" object in the body, not a "header". 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文