-2

I am trying to log into my spotify account using my username and password using requests and 2captcha. When I run my code, I am returned with b'{"error":"errorInvalidCredentials"}' and am unable to login.

I used my personal login and extract the csrf token from my cookies for the post requests payload.

Wondering if the data in the post request is being passed through correctly.

from twocaptcha import TwoCaptcha
from email import header
import json
import requests
 
s = requests.Session()
 
headers = { 
    'documentLifecycle':'active',
    'frameType':'outermost_frame',
    'initiator':'https://accounts.spotify.com',
    'method':'GET',
    'url':'https://accounts.spotify.com/en/login/?continue=https%3A//www.spotify.com/us/account/overview/&_locale=en-US',
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'en-US,en;q=0.9',
    'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"',
    'Sec-Fetch-Dest':'document',
    'Sec-Fetch-Mode':'navigate',
    'Sec-Fetch-Site':'same-origin',
    'Sec-Fetch-User':'?1',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
}
 
r = s.get('https://accounts.spotify.com/en/login/?continue=https%3A//www.spotify.com/us/account/overview/&_locale=en-US', headers=headers, allow_redirects=True)
 
print(r.status_code)
print(r.content)
print(r.history)
 
c = r.cookies
csrf = c.items()[1][1]
 
csrf = str(csrf)
 
print(csrf)
 
headers = {
    'documentLifecycle':'active',
    'frameType':'outermost_frame',
    'initiator':'https://accounts.spotify.com',
    'method':'POST',
    'url':'https://accounts.spotify.com/login/password',
    'Accept':'application/json',
    'Accept-Encoding':'gzip, deflate, br',
    'Accept-Language':'en-US,en;q=0.9',
    'Content-Type':'application/x-www-form-urlencoded',
    'Origin':'https://accounts.spotify.com',
    'Referer':'https://accounts.spotify.com/en/login/?continue=https%3A//www.spotify.com/us/account/overview/&_locale=en-US',
    'sec-ch-ua':'" Not A;Brand";v="99", "Chromium";v="101", "Google Chrome";v="101"',
    'sec-ch-ua-mobile':'?0',
    'sec-ch-ua-platform':'"Windows"',
    'Sec-Fetch-Dest':'empty',
    'Sec-Fetch-Mode':'cors',
    'Sec-Fetch-Site':'same-origin',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36',
    'X-CSRF-Token': csrf
}
 
solver = TwoCaptcha('xxxx')
 
try:
    result = solver.recaptcha(
        sitekey="6LfCVLAUAAAAALFwwRnnCJ12DalriUGbj8FW_J39",
        url='https://accounts.spotify.com/',
        version="V3",
        id="100000")
 
except Exception as e:
    print('Error')
 
else:
    print('solved') 
    captcha = result
    print(captcha)
 
print(captcha['code'])
 
username = 'xxxx'
password = 'xxxx'
 
data = {
    'username': username,
    'password': password,
    'remember': 'true',
    'recaptchaToken': captcha['code'],
    'continue': 'https://www.spotify.com/us/account/overview/'
}
 
r = s.post('https://accounts.spotify.com/login/password', json=data, headers=headers, allow_redirects=True)
 
print(r.status_code)
print(r.content)
exe
  • 354
  • 3
  • 23
  • depending on what your eventual plan is could you use the Spotify API? https://developer.spotify.com/console/users-profile/ – Andrew Ryan May 30 '22 at 02:02
  • @AndrewRyan The spotify api doesn't have all the capabilities I need for my project. Ontop of being able to use the functions the spotify api has, I need to be able to access the subscription plan the user has and be able to cancel, upgrade, and downgrade. – exe May 30 '22 at 20:32

1 Answers1

1

Have you had a look at spotipy? This is what I was using when playing around with the spotify API.

The examples from the website:

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

birdy_uri = 'spotify:artist:2WX2uTcsvV5OnS0inACecP'
spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())

results = spotify.artist_albums(birdy_uri, album_type='album')
albums = results['items']
while results['next']:
    results = spotify.next(results)
    albums.extend(results['items'])

for album in albums:
    print(album['name'])
mortom123
  • 511
  • 4
  • 14