【问题标题】:401 Client Error: Unauthorized for url401 客户端错误:未经授权的 url
【发布时间】:2018-07-07 22:19:54
【问题描述】:

最近开始吃

requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://api.soundcloud.com/oauth2/token

使用 soundcloud (0.5.0) Python 库。

它发生在

client = soundcloud.Client(client_id='id',
                           client_secret='secret',
                           username='user@mail.com',
                           password='passwd')

我仔细检查了我的凭据以确保它们不是原因。我试图从不同的 IP 和不同的机器获取客户端实例。在一天中的某些随机时间,我可以获得一个 Client 实例,但 99.99% 的时间我都会收到错误。

该错误是否意味着我因某种原因被禁止?

【问题讨论】:

  • 通常 401 是您未授权特定资源。
  • 我明白了,但我不知道为什么。
  • 这意味着您发送的凭据有误,请验证您的凭据。
  • 我在最后几天至少验证了 10 次。我确实可以通过网络浏览器使用相同的凭据登录,但不能通过 API。
  • 你知道client-id和client-secret吗?一般来说,client-secret 是密码,client-id 是您的本地帐户名。

标签: python exception soundcloud http-error


【解决方案1】:

这可能会有所帮助,我通过从配置文件中读取用户名和密码解决了类似的问题。

试试:

# config.ini

[my_app]
CLIENT_ID     = enter_your_id
CLIENT_SECRET = enter_your_secret
USERNAME      = enter_username
PASSWORD      = enter_password

从 Python 3.8 为 Python 2.6+ 安装 configparserpip install configparser。 有关详细信息,请参阅伟大的 documentation

import configparser

config = configparser.RawConfigParser()
config.read('config.ini')


my_id = config.get('my_app', 'CLIENT_ID')
my_secret = config.get('my_app', 'CLIENT_SECRET')
my_user = config.get('my_app', 'USERNAME')
my_pass = config.get('my_app', 'PASSWORD')


client = soundcloud.Client(client_id=my_id,
                           client_secret=my_secret,
                           username=my_user,
                           password=my_pass)

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2021-01-07
    • 2022-10-25
    • 1970-01-01
    • 2013-08-03
    • 1970-01-01
    相关资源
    最近更新 更多