11

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='user@mail.com',
                           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?

Alex
  • 111
  • 1
  • 1
  • 3
  • 1
    In general 401 is your not authorized for the particular resource. – kvk30 Jan 29 '18 at 09:17
  • 1
    I see but I can't figure out why. – Alex Jan 29 '18 at 09:24
  • 1
    It means you are sending wrong credentials please verify your credentials. – kvk30 Jan 29 '18 at 09:57
  • 2
    I verified it at least 10 times during the last days. I do can login using the same credentials via web browsers, but not via the API. – Alex Jan 29 '18 at 10:10
  • 1
    Do you know client-id and client-secret? In general client-secret is password and client-id is your local account name. – kvk30 Jan 29 '18 at 10:12
  • Yes, I do know both id and secret. I have several pairs and all of them experience the same difficulties. – Alex Jan 29 '18 at 10:59
  • Oh then once raise a request to sound-cloud support team. They can help you better than me. – kvk30 Jan 29 '18 at 11:33
  • It is said on their support page (https://developers.soundcloud.com/support) that they answer all the questions just right here on SO. This is why I posted my question here. Do I miss something? – Alex Jan 29 '18 at 12:27
  • No, It was good to post here. – kvk30 Jan 30 '18 at 05:32
  • Having the same issue as well. Soundcloud seems to rate-limit the password credentials grant after 10 requests in a short time. Not sure how long the rate limit is or if it is indefinite. – D Malan Oct 20 '20 at 18:20

2 Answers2

1

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)
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
  • 1
    Configparser is in the standard library, there should be no need to install it. – snakecharmerb Oct 09 '22 at 18:43
  • 1
    Yes. Also, this package is a backport of the refreshed and enhanced ConfigParser from later Python versions. To use the backport instead of the built-in version, simply import it explicitly as a backport: `from backports import configparser` – Milovan Tomašević Oct 09 '22 at 19:11
-1

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.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 13 '22 at 02:58