使用python登录spotify



我正在尝试使用请求和2captcha使用我的用户名和密码登录我的spotify帐户。当我运行代码时,返回b'{"error":"errorInvalidCredentials"}',无法登录。

我使用我的个人登录并从我的cookie中提取csrf令牌用于post请求有效负载。

想知道post请求中的数据是否被正确传递。

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)

你看过spotipy了吗?这是我在使用spotify API时使用的。

来自网站的例子:

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'])

相关内容

  • 没有找到相关文章

最新更新