You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

咨询Python通过邮箱密码登录Spotify获取Bearer Token的实现方法

Spotify 邮箱密码登录获取Bearer Token方案

方案一:纯requests实现说明

纯requests模拟登录的方案基本不可行,核心原因如下:

  • Spotify登录流程包含动态CSRF令牌、设备指纹校验,密码提交前会经过前端JS加密,加密逻辑随版本迭代频繁更新,很难稳定复现加密逻辑
  • 登录过程有多步302重定向校验,部分校验参数仅在前端运行时生成,无法通过静态抓包固定所有请求参数
  • 平台内置人机检测机制,纯接口请求大概率会被拦截,返回403错误或要求完成验证码/滑块校验,普通开发者几乎无法稳定绕过

方案二:Selenium模拟登录实现(可稳定复现)

前置依赖安装

执行命令安装所需包:
pip install selenium webdriver-manager

实现步骤

  1. 初始化Chrome浏览器驱动,开启性能日志监听能力,用于捕获网络请求头
  2. 跳转Spotify官方登录页,自动填入邮箱、密码后点击登录,若遇验证码可加手动等待时间完成校验
  3. 登录成功后跳转Spotify网页端首页,触发携带Bearer Token的业务请求
  4. 过滤所有网络请求的请求头,提取Authorization字段中Bearer 开头的Token值

参考代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
import time
import json

# 配置浏览器参数,开启性能日志记录
chrome_options = webdriver.ChromeOptions()
chrome_options.set_capability('goog:loggingPrefs', {'performance': 'ALL'})
# 初始化驱动
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)

try:
    # 打开登录页面
    driver.get("https://accounts.spotify.com/zh-CN/login")
    time.sleep(2)
    # 填入账号信息
    driver.find_element(By.ID, "login-username").send_keys("你的Spotify绑定邮箱/用户名")
    driver.find_element(By.ID, "login-password").send_keys("你的账号密码")
    # 点击登录
    driver.find_element(By.ID, "login-button").click()
    # 此处可调整等待时间,遇到验证码时手动完成校验
    time.sleep(8)
    
    # 跳转网页端播放器页面,触发业务请求
    driver.get("https://open.spotify.com/")
    time.sleep(3)
    
    # 遍历所有网络请求日志提取Token
    request_logs = driver.get_log('performance')
    target_token = None
    for log_entry in request_logs:
        log_msg = json.loads(log_entry['message'])['message']
        if log_msg['method'] == 'Network.requestWillBeSentExtraInfo':
            req_headers = log_msg['params']['headers']
            if 'authorization' in req_headers and req_headers['authorization'].startswith('Bearer '):
                target_token = req_headers['authorization']
                break
    
    if target_token:
        print(f"提取到的Bearer Token:{target_token}")
    else:
        print("未检测到Bearer Token,请确认登录状态是否正常")
finally:
    driver.quit()

注意事项

  • 若登录时触发人机校验,可将登录后的等待时间调整为15-20秒,手动完成校验后程序会自动继续执行
  • 不同权限的Bearer Token有效期不同,通常为1-24小时,过期后需要重新登录获取
  • 不要短时间内频繁触发重复登录操作,避免账号被平台临时限制登录

内容的提问来源于stack exchange,提问作者Emrovsky Emrovsky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 17:45:03