Search code examples
pythonspotify

Spotify API "No token provided" error code 401


While using Spotify API to extract user's top tracks, I've been receiving 401 error consistently. The code is shown below:

import json
from requests import post, get
import base64

def get_token():
    auth_string = CLIENT_ID + ":" + CLIENT_SECRET
    auth_bytes = auth_string.encode("utf-8")
    auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")

    url = "https://accounts.spotify.com/api/token"
    headers = {
        "Authorization" : "Basic " + auth_base64,
        "Content-Type" : "application/x-www-form-urlencoded"
    } 

    data = {"grant_type" : "client_credentials"}
    result = post(url, headers=headers, data=data)
    json_result = json.loads(result.content)
    token = json_result["access_token"]
    return token

def searchArtist(token, artist_name):
    url = "https://api.spotify.com/v1/search"
    headers = {
        "Authorizations" : "Bearer " + token
    }
    query = f"?q={artist_name}&type=artist&limit=1"
    query_url = url + query
    result = get(query_url, headers=headers)
    data = result.json()
    print(data)

myToken = get_token()
searchArtist(myToken, "ACDC")

The following error is shown:

{'error': {'status': 401, 'message': 'No token provided'}}

Am I missing something?

I was expecting the tracks to show. I've looked upon Spotify API docs and on my end I've been doing everything correctly. Probably I'm missing something or what is the actual, I can't tell.


Solution

  • In the searchArtist you added Authorizations and it is not correct, it is Authorization without s at the end.