[GH-ISSUE #288] BadCredentials regardless of username and password #54

Closed
opened 2026-02-27 08:11:39 +03:00 by kerem · 1 comment
Owner

Originally created by @greffgreff on GitHub (Nov 22, 2024).
Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/288

Describe the bug
A clear and concise description of what the bug is.

I am unable to authenticate with valid spotify username/password or valid email/password. I am getting the following error:

  File "[...]\spotify_downloader.py", line 126, in <module>
    main()
  File "[...]\spotify_downloader.py", line 108, in main
    session = Session.Builder().user_pass(username, password).create()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 1287, in create
    session.authenticate(self.login_credentials)
  File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 709, in authenticate
    self.__authenticate_partial(credential, False)
  File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 1067, in __authenticate_partial
    raise Session.SpotifyAuthenticationException(ap_login_failed)
librespot.core.Session.SpotifyAuthenticationException: BadCredentials

Using both genuine and burner accounts produces the same error.

To reproduce

I am getting the above error with the following:

Session.Builder().user_pass("<username/email>", "<password>").create()

Below is the entirety of the script I am using:

# pip install requests librespot pydub music_tag ffmpeg

import os
import sys
import requests
import argparse
from librespot.core import Session
from librespot.metadata import TrackId
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality
from pydub import AudioSegment
import music_tag

quality = AudioQuality.HIGH


def get_track_info(track_id, session):
    access_token = session.tokens().get('playlist-read')

    url = f"https://api.spotify.com/v1/tracks/{track_id}"
    headers = {
        "Authorization": f"Bearer {access_token}"
    }

    response = requests.get(url, headers=headers)
    if response.status_code != 200:
        raise Exception(
            f"Failed to get track info: {response.status_code} - {response.text}")

    return response.json()


def fetch_audio_stream(session, track_id, quality):
    track_id = TrackId.from_uri("spotify:track:" + track_id)
    stream = session.content_feeder().load(
        track_id, VorbisOnlyAudioQuality(quality), False, None)
    if stream is not None:
        return stream.input_stream.stream()
    raise RuntimeError("No stream available.")


def download_to_file(stream, filename):
    with open(filename, 'wb') as f:
        while True:
            chunk = stream.read(4096)
            if not chunk:
                break
            f.write(chunk)


def ogg_to_mp3(filename):
    raw_audio = AudioSegment.from_file(filename, format="ogg",
                                       frame_rate=44100, channels=2, sample_width=2)
    raw_audio.export(filename, format="mp3")


def set_audio_tags(filename, track):
    f = music_tag.load_file(filename)
    if f is None or track is None:
        return
    if 'album' in track and 'images' in track['album'] and len(track['album']['images']) > 0:
        art = requests.get(track['album']['images'][0]['url']).content
        f['artwork'] = art
    if 'artists' in track:
        artists = [artist['name'] for artist in track['artists']]
        parsed_artists = ', '.join(artists)
        f['artist'] = parsed_artists
    if 'name' in track:
        f['tracktitle'] = track['name']
    if 'album' in track and 'name' in track['album']:
        f['album'] = track['album']['name']
    if 'album' in track and 'release_date' in track['album']:
        year = track['album']['release_date'].split("-")[0]
        f['year'] = year
    if 'disc_number' in track:
        f['discnumber'] = track['disc_number']
    if 'track_number' in track:
        f['tracknumber'] = track['track_number']
    f['comment'] = """
   /\\_/\\
 ( o.o )
  > ^ <
"""
    f.save()


def main():
    parser = argparse.ArgumentParser(description='Downloads a Spotify track.')
    parser.add_argument('--username', type=str,
                        required=True, help='Spotify username')
    parser.add_argument('--password', type=str,
                        required=True, help='Spotify password')
    parser.add_argument('--out_directory', type=str,
                        required=True, help='Download directory')
    parser.add_argument('--track', type=str, required=True,
                        help='Spotify track id')

    args = parser.parse_args()

    track_id = args.track
    username = args.username
    password = args.password
    out_directory = args.out_directory

    if not os.path.isdir(out_directory):
        os.makedirs(out_directory)

    session = Session.Builder().user_pass(username, password).create()

    track = get_track_info(track_id, session)
    artists = [artist['name'] for artist in track['artists']]
    title = ', '.join(artists) + " - " + track["name"]
    filename = os.path.join(out_directory, title + '.mp3')

    if os.path.isfile(filename):
        sys.stdout.buffer.write(filename.encode("utf-8"))
    else:
        stream = fetch_audio_stream(session, track_id, quality)
        download_to_file(stream, filename)
        ogg_to_mp3(filename)
        set_audio_tags(filename, track)
        sys.stdout.buffer.write(filename.encode("utf-8"))


if __name__ == "__main__":
    main()

Which can be called using

python spotify_downloader.py --username <valid username> --password <valid password> --track 6FYobREsyfp4SgtpnQbnaf --out_directory ./out

Expected behavior
Upon successful execution, a track should be downloaded and playable under the ./out directory

Client Information (please complete the following information):

  • OS: Windows 11
  • Python Version: 3.12.6
  • Library Version: 0.0.9
Originally created by @greffgreff on GitHub (Nov 22, 2024). Original GitHub issue: https://github.com/kokarare1212/librespot-python/issues/288 **Describe the bug** A clear and concise description of what the bug is. I am unable to authenticate with valid spotify username/password or valid email/password. I am getting the following error: ``` File "[...]\spotify_downloader.py", line 126, in <module> main() File "[...]\spotify_downloader.py", line 108, in main session = Session.Builder().user_pass(username, password).create() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 1287, in create session.authenticate(self.login_credentials) File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 709, in authenticate self.__authenticate_partial(credential, False) File "[...]\AppData\Roaming\Python\Python312\site-packages\librespot\core.py", line 1067, in __authenticate_partial raise Session.SpotifyAuthenticationException(ap_login_failed) librespot.core.Session.SpotifyAuthenticationException: BadCredentials ``` Using both genuine and burner accounts produces the same error. **To reproduce** I am getting the above error with the following: ```python Session.Builder().user_pass("<username/email>", "<password>").create() ``` Below is the entirety of the script I am using: ```python # pip install requests librespot pydub music_tag ffmpeg import os import sys import requests import argparse from librespot.core import Session from librespot.metadata import TrackId from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality from pydub import AudioSegment import music_tag quality = AudioQuality.HIGH def get_track_info(track_id, session): access_token = session.tokens().get('playlist-read') url = f"https://api.spotify.com/v1/tracks/{track_id}" headers = { "Authorization": f"Bearer {access_token}" } response = requests.get(url, headers=headers) if response.status_code != 200: raise Exception( f"Failed to get track info: {response.status_code} - {response.text}") return response.json() def fetch_audio_stream(session, track_id, quality): track_id = TrackId.from_uri("spotify:track:" + track_id) stream = session.content_feeder().load( track_id, VorbisOnlyAudioQuality(quality), False, None) if stream is not None: return stream.input_stream.stream() raise RuntimeError("No stream available.") def download_to_file(stream, filename): with open(filename, 'wb') as f: while True: chunk = stream.read(4096) if not chunk: break f.write(chunk) def ogg_to_mp3(filename): raw_audio = AudioSegment.from_file(filename, format="ogg", frame_rate=44100, channels=2, sample_width=2) raw_audio.export(filename, format="mp3") def set_audio_tags(filename, track): f = music_tag.load_file(filename) if f is None or track is None: return if 'album' in track and 'images' in track['album'] and len(track['album']['images']) > 0: art = requests.get(track['album']['images'][0]['url']).content f['artwork'] = art if 'artists' in track: artists = [artist['name'] for artist in track['artists']] parsed_artists = ', '.join(artists) f['artist'] = parsed_artists if 'name' in track: f['tracktitle'] = track['name'] if 'album' in track and 'name' in track['album']: f['album'] = track['album']['name'] if 'album' in track and 'release_date' in track['album']: year = track['album']['release_date'].split("-")[0] f['year'] = year if 'disc_number' in track: f['discnumber'] = track['disc_number'] if 'track_number' in track: f['tracknumber'] = track['track_number'] f['comment'] = """ /\\_/\\ ( o.o ) > ^ < """ f.save() def main(): parser = argparse.ArgumentParser(description='Downloads a Spotify track.') parser.add_argument('--username', type=str, required=True, help='Spotify username') parser.add_argument('--password', type=str, required=True, help='Spotify password') parser.add_argument('--out_directory', type=str, required=True, help='Download directory') parser.add_argument('--track', type=str, required=True, help='Spotify track id') args = parser.parse_args() track_id = args.track username = args.username password = args.password out_directory = args.out_directory if not os.path.isdir(out_directory): os.makedirs(out_directory) session = Session.Builder().user_pass(username, password).create() track = get_track_info(track_id, session) artists = [artist['name'] for artist in track['artists']] title = ', '.join(artists) + " - " + track["name"] filename = os.path.join(out_directory, title + '.mp3') if os.path.isfile(filename): sys.stdout.buffer.write(filename.encode("utf-8")) else: stream = fetch_audio_stream(session, track_id, quality) download_to_file(stream, filename) ogg_to_mp3(filename) set_audio_tags(filename, track) sys.stdout.buffer.write(filename.encode("utf-8")) if __name__ == "__main__": main() ``` Which can be called using ```python python spotify_downloader.py --username <valid username> --password <valid password> --track 6FYobREsyfp4SgtpnQbnaf --out_directory ./out ``` **Expected behavior** Upon successful execution, a track should be downloaded and playable under the ./out directory **Client Information (please complete the following information):** - OS: Windows 11 - Python Version: 3.12.6 - Library Version: 0.0.9
kerem 2026-02-27 08:11:39 +03:00
Author
Owner

@kokarare1212 commented on GitHub (Nov 22, 2024):

Same issue
https://github.com/kokarare1212/librespot-python/issues/277

<!-- gh-comment-id:2493967058 --> @kokarare1212 commented on GitHub (Nov 22, 2024): Same issue https://github.com/kokarare1212/librespot-python/issues/277
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/librespot-python-kokarare1212#54
No description provided.