跳转至

utils.credential

凭据类,用于请求验证

Credential dataclass

Credential(openid: str = '', refresh_token: str = '', access_token: str = '', expired_at: int = 0, musicid: int = 0, musickey: str = '', unionid: str = '', str_musicid: str = '', refresh_key: str = '', encrypt_uin: str = '', login_type: int = 0, extra_fields: dict[str, Any] = dict())

凭据类

ATTRIBUTE DESCRIPTION
openid

OpenID

TYPE: str

refresh_token

RefreshToken

TYPE: str

access_token

AccessToken

TYPE: str

expired_at

到期时间

TYPE: int

musicid

QQMusicID

TYPE: int

musickey

QQMusicKey

TYPE: str

unionid

UnionID

TYPE: str

str_musicid

QQMusicID

TYPE: str

refresh_key

RefreshKey

TYPE: str

login_type

登录类型

TYPE: int

extra_fields

额外字段

TYPE: dict[str, Any]

has_musicid

has_musicid() -> bool

是否提供 musicid

Source code in qqmusic_api/utils/credential.py
def has_musicid(self) -> bool:
    """是否提供 musicid"""
    return bool(self.musicid)

has_musickey

has_musickey() -> bool

是否提供 musickey

Source code in qqmusic_api/utils/credential.py
def has_musickey(self) -> bool:
    """是否提供 musickey"""
    return bool(self.musickey)

raise_for_invalid

raise_for_invalid() -> None

检查凭据是否有效

RAISES DESCRIPTION
CredentialInvalidError

没有提供 musicid 或 musickey

Source code in qqmusic_api/utils/credential.py
def raise_for_invalid(self) -> None:
    """检查凭据是否有效

    Raises:
        CredentialInvalidError: 没有提供 musicid 或 musickey
    """
    if not self.has_musicid():
        raise CredentialInvalidError("没有提供 musicid")

    if not self.has_musickey():
        raise CredentialInvalidError("没有提供 musickey")

refresh async

refresh() -> bool

刷新 cookies

RETURNS DESCRIPTION
bool

是否刷新成功

Source code in qqmusic_api/utils/credential.py
async def refresh(self) -> bool:
    """刷新 cookies

    Returns:
        是否刷新成功
    """
    from ..login import refresh_cookies

    c = await refresh_cookies(self)
    if c == self:
        return False
    self.__dict__.update(c.__dict__)
    return True

can_refresh async

can_refresh() -> bool

是否可以刷新 credential

Source code in qqmusic_api/utils/credential.py
async def can_refresh(self) -> bool:
    """是否可以刷新 credential"""
    if not self.has_musicid() or not self.has_musickey():
        return False
    if await self.is_expired():
        return bool(self.refresh_key) and bool(self.refresh_token)
    return True

is_expired async

is_expired() -> bool

判断 credential 是否过期

RETURNS DESCRIPTION
bool

是否过期

Source code in qqmusic_api/utils/credential.py
async def is_expired(self) -> bool:
    """判断 credential 是否过期

    Returns:
        是否过期
    """
    from ..login import check_expired

    return await check_expired(self)

as_dict

as_dict() -> dict

获取凭据字典

Source code in qqmusic_api/utils/credential.py
def as_dict(self) -> dict:
    """获取凭据字典"""
    d = asdict(self)
    d["loginType"] = d.pop("login_type")
    d["encryptUin"] = d.pop("encrypt_uin")
    return d

as_json

as_json() -> str

获取凭据 JSON 字符串

Source code in qqmusic_api/utils/credential.py
def as_json(self) -> str:
    """获取凭据 JSON 字符串"""
    data = self.as_dict()
    data.update(data.pop("extra_fields"))
    return json.dumps(data, indent=4, ensure_ascii=False)

from_cookies_dict classmethod

from_cookies_dict(cookies: dict[str, Any]) -> Self

从 cookies 字典创建 Credential 实例

PARAMETER DESCRIPTION
cookies

Cookies 字典

TYPE: dict[str, Any]

RETURNS DESCRIPTION
Self

凭据类实例

Source code in qqmusic_api/utils/credential.py
@classmethod
def from_cookies_dict(cls, cookies: dict[str, Any]) -> Self:
    """从 cookies 字典创建 Credential 实例

    Args:
        cookies: Cookies 字典

    Returns:
        凭据类实例
    """
    return cls(
        openid=cookies.pop("openid", ""),
        refresh_token=cookies.pop("refresh_token", ""),
        access_token=cookies.pop("access_token", ""),
        expired_at=cookies.pop("expired_at", 0),
        musicid=cookies.pop("musicid", 0),
        musickey=cookies.pop("musickey", ""),
        unionid=cookies.pop("unionid", ""),
        str_musicid=cookies.pop(
            "str_musicid",
            str(cookies.pop("musicid", "")),
        ),
        refresh_key=cookies.pop("refresh_key", ""),
        encrypt_uin=cookies.pop("encryptUin", ""),
        login_type=cookies.pop("loginType", 0),
        extra_fields=cookies,
    )

from_cookies_str classmethod

from_cookies_str(cookies: str) -> Self

从 cookies 字符串创建 Credential 实例

PARAMETER DESCRIPTION
cookies

Cookies 字符串

TYPE: str

RETURNS DESCRIPTION
Self

凭据类实例

Source code in qqmusic_api/utils/credential.py
@classmethod
def from_cookies_str(cls, cookies: str) -> Self:
    """从 cookies 字符串创建 Credential 实例

    Args:
        cookies: Cookies 字符串

    Returns:
        凭据类实例
    """
    return cls.from_cookies_dict(json.loads(cookies))