#🔒 401 - A request must contain the Authorization header. Soundcloud

29 messages · Page 1 of 1 (latest)

tawny raptor
#

Hey there!
Recently got soundcloud api key and when i try to get tracks by search query i get error 401 - A request must contain the Authorization header.
There is my code

import requests

CLIENT_ID = "id"  

url = 'https://api.soundcloud.com/tracks/?client_id='+CLIENT_ID+'&q=Cahalot'

print(requests.get(url).content)

Can you help me find problem that result this?

coarse lakeBOT
#

@tawny raptor

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

tawny raptor
#

Error stack

{
  "code": 401,
  "message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api.",
  "link": "https://developers.soundcloud.com/docs/api/explorer/open-api",
  "status": "401 - Unauthorized",
  "errors": [
    {
      "error_message": "A request must contain the Authorization header. For details please refer to https://developers.soundcloud.com/blog/security-updates-api."
    }
  ],
  "error": null
}
tawny raptor
#

Up btw

ember gazelle
#

Uh, the error is very clear for someone who knows requests. You're not even trying to add any header.

Do you know how to add headers? You can pass them as dict in the request.get call as keyword argument headers=

#

As the link in the error says https://developers.soundcloud.com/blog/security-updates-api
The header should be just "Authorization" with content being "OAuth <your token here>"

tawny raptor
tawny raptor
tawny raptor
ember gazelle
tawny raptor
#
import requests

headers = {
    'Authorization': 'OAuth token
',
}

params = {
    'limit': '1',
}

response = requests.get('https://api.soundcloud.com/me/tracks', params=params, headers=headers)

print(response.content

Stack

b'{"code":401,"message":"","link":"https://developers.soundcloud.com/docs/api/explorer/open-api","status":"401 - Unauthorized","errors":[],"error":null}'
ember gazelle
tawny raptor
ember gazelle
ember gazelle
tawny raptor
tawny raptor
ember gazelle
ember gazelle
#

Are you trying to log in every time and get new token, instead of generating token and reusing it? Because 1h limit sounds like some login limit, not "i want to revoke my token because I posted it publicly" interface

tawny raptor
tawny raptor
ember gazelle
#

Please read what each token does and don't just randomly throw them into requests

tawny raptor
ember gazelle
#

And there should be a way to log out the session to invalidate the previous token. Don't use refresh token when not needed

tawny raptor
novel heath
coarse lakeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.