Hai Hoang Thanh
Hai Hoang Thanh

Reputation: 1

Python - Spotify API: Error 401, "no token provided"

I'm trying to follow the Youtube tutorial to extract data from Spotify API. I compared my code to the one that's being shared in the video and they look exactly the same, but I keep on getting this error:

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

For reference, my python is here:

import sqlalchemy
import pandas as pd
from sqlalchemy.orm import sessionmaker
import requests
import json
from datetime import datetime
import datetime
import sqlite3

DATABASE_LOCATION = "sqlite:///my_played_tracks.sqlite"
USER_ID = '31bkmllknst2codaz4pqdksjrlha'
TOKEN = 'BQASJqXY4P89Rg8Uxy9IJXgMnW9HP3Kuz7Yuk3QeVOrDJskJqqP5Y35eOx_VUYj9xsygDFgx5HF5NEyx4ZFLyGvR4N5ihFw1dzqjl4gfoxvk3smuUl5FU5QPitxHo69qES006a-kY-kltSo0k7-7zLXrCC4LSakGGQQNPlF'

if __name__ == '__main__':

    headers = {
        "Accept" : "application/json",
        "Content-Type" : "application/json",
        "Authorization" : "Bearer {token}".format(token=TOKEN)
    }

    today = datetime.datetime.now()
    yesterday = today - datetime.timedelta(days=1)
    yesterday_unix_timestamp = int(yesterday.timestamp()) * 1000

    r = requests.get("https://api.spotify.com/v1/me/player/recently-played?after{time}".format(time=yesterday_unix_timestamp),
    headers=headers)

    data = r.json()

    print(data)

Upvotes: 0

Views: 1929

Answers (1)

WildSiphon
WildSiphon

Reputation: 140

I'm guessing that you posted your own token and not the ones in the video (if so, that is your problem). You should know that's a bad idea to share credentials token on the internet, I would suggest you to edit your question to hide them.

Anyway, I tried your code with tokens you provided and I didn't get the same error than you. Instead of "No token provided" I got "Invalid access token".

I don't know what you tried before but when this kind of problems happened you could:

  • (re)generate your API token in spotify for developpers
  • wait 10 minutes before trying again just to be sure that your token is active

Upvotes: 1

Related Questions