登录
示例:手机号登录
import asyncio
import sys
from qqmusic_api.login_utils import PhoneLogin, PhoneLoginEvents
async def phone_login():
phone = input("请输入手机号码")
login = PhoneLogin(int(phone))
while 1:
state = await login.send_authcode()
if state == PhoneLoginEvents.SEND:
print("发送成功")
break
if state == PhoneLoginEvents.CAPTCHA:
if login.auth_url is None:
print("获取滑块验证链接失败")
return
print("需要滑块验证", login.auth_url)
if sys.platform == "win32":
await asyncio.create_subprocess_exec("start", login.auth_url, shell=True)
elif sys.platform == "darwin":
await asyncio.create_subprocess_exec("open", login.auth_url)
else:
await asyncio.create_subprocess_exec("xdg-open", login.auth_url)
print("验证后回车")
await asyncio.sleep(0)
else:
print("未知情况", login.error_msg)
return
code = int(input("请输入验证码"))
credential = await login.authorize(code)
print(credential)
asyncio.run(phone_login())
示例:二维码登录
import asyncio
from qqmusic_api.login_utils import QQLogin, QRCodeLogin, QrCodeLoginEvents
def show_qrcode(data: bytes):
"""显示二维码"""
try:
from io import BytesIO
from PIL import Image
from pyzbar.pyzbar import decode
from qrcode import QRCode # type: ignore
img = Image.open(BytesIO(data))
url = decode(img)[0].data.decode("utf-8")
qr = QRCode()
qr.add_data(url)
qr.print_ascii()
except ImportError:
with open("qrcode.png", "wb") as f:
f.write(data)
print("请在 qrcode.png 中查看二维码")
async def qrcode_login(login: QRCodeLogin):
"""二维码登录"""
print(login.__class__.__name__)
show_qrcode(await login.get_qrcode())
while True:
state, credential = await login.check_qrcode_state()
if state == QrCodeLoginEvents.REFUSE:
print("拒绝登录")
exit()
elif state == QrCodeLoginEvents.TIMEOUT:
print("二维码过期")
exit()
elif state == QrCodeLoginEvents.CONF:
print("\r请确认登录", end="")
elif state == QrCodeLoginEvents.SCAN:
print("\r请扫描二维码", end="")
elif state == QrCodeLoginEvents.DONE:
print("\n登录成功")
break
await asyncio.sleep(4)
# 获取Credential
# login.credential
print(credential)
asyncio.run(qrcode_login(QQLogin()))