Alex
Alex

Reputation: 131

401 Client Error: Unauthorized for url

Recently I started to get

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

using soundcloud (0.5.0) Python library.

It happens in

client = soundcloud.Client(client_id='id',
                           client_secret='secret',
                           username='[email protected]',
                           password='passwd')

I double checked my credentials to make sure they are not the cause. I tried to get a Client instance from different IPs and different machines. At some random times during a day I can get a Client instance, but 99.99% of the day I get the error.

Does the error mean I was banned for some reason?

Upvotes: 13

Views: 60348

Answers (2)

Kurdish Learner
Kurdish Learner

Reputation: 75

Probably you are using a third-party app to access your account, in case you are using Gmail service for your purpose, consider allowing third-party apps to have access to your Gmail account, how are you going to do that:

  • Go to https://myaccount.google.com/security
  • Use Two-Step-Authentication and enable it.
  • Create an App Password, Select App > Mail | Select Device > Windows Computer.
  • And use the password given

And that should solve your issue.

Upvotes: 0

Milovan Tomašević
Milovan Tomašević

Reputation: 8673

It may be helpful, I solved a similar problem by reading the username and password from the configuration file.

Try:

# config.ini

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

Install configparser from Python 3.8 for Python 2.6+: pip install configparser. See the great documentation for more details.

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)

Upvotes: 1

Related Questions