Monday, 30 May 2022

Log into spotify using python

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)


from Log into spotify using python

No comments:

Post a Comment