快手扫码登陆

快手扫码登陆

1.二维码生成接口:

https://id.kuaishou.com/rest/c/infra/ks/qr/start

快手扫码登陆

响应值imageData为base64编码的二维码图片

快手扫码登陆

2.检测过期状态

https://id.kuaishou.com/rest/c/infra/ks/qr/scanResult

3.效验Login

https://id.kuaishou.com/rest/c/infra/ks/qr/acceptResult
快手扫码登陆

4.回调登陆接口:

https://id.kuaishou.com/pass/kuaishou/login/qr/callback

快手扫码登陆

5.上代码:

# -*- coding: utf-8 -*-
# @Author  : Codeooo
# @Time    : 2021/11/15




import base64
import requests

from requests import RequestException


class KsLogin:

    session = requests.Session()

    def __init__(self):

        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/80.0',
            'Accept': '*/*',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
            'Referer': 'https://www.kuaishou.com/',
            'Content-type': 'application/x-www-form-urlencoded',
            'Origin': 'https://www.kuaishou.com',
            'Connection': 'keep-alive',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-site',
            'Pragma': 'no-cache',
            'Cache-Control': 'no-cache',
        }



    def postRequests(self, url: str, data) -> str:

        for _ in range(5):
            try:
                res = self.session.post(
                    url=url,
                    headers=self.headers,
                    data=data,
                    proxies=self.proxies,
                    timeout=5
                )
            except RequestException:
                # self.proxies = rodmoProxy()
                # TODO 请求抛错未处理
                pass
                # time.sleep(random.randint(1, 3))
            else:
                return res.json()

    def authToken(self, token):

        authTokenUrl = f'https://www.kuaishou.com/rest/infra/sts?authToken={token}&sid=kuaishou.server.web'
        print("=====authTokenUrl=====:", authTokenUrl)

        for _ in range(5):
            try:
                res = self.session.get(
                    url=authTokenUrl,
                    headers=self.headers,
                    proxies=self.proxies,
                    timeout=5
                )
            except RequestException:
                pass
                # self.proxies = rodmoProxy()
                # TODO 请求抛错未处理
                # time.sleep(random.randint(1, 3))
            else:
                print(res.cookies.get_dict())
                html = res.json()
                return html
        return

    def main(self):


        """proxy测试"""
        # self.proxies = rodmoProxy()
        self.proxies = None


        # 二维码
        qrCode = self.postRequests('https://id.kuaishou.com/rest/c/infra/ks/qr/start',  {"sid": 'kuaishou.server.web'})
        print("=====生成二维码=====:",qrCode)
        qrLoginToken = qrCode.get("qrLoginToken")
        qrLoginSignature = qrCode.get("qrLoginSignature")

        with open('qrCode.jpg', 'wb') as file:
            file.write(base64.b64decode(qrCode.get("imageData")))

        input("请扫码二维码:Y/N")


        data_accept = {
            'qrLoginToken': qrLoginToken,
            'qrLoginSignature': qrLoginSignature,
            'sid': 'kuaishou.server.web'
        }


        # 效验Login
        acceptResult = self.postRequests('https://id.kuaishou.com/rest/c/infra/ks/qr/acceptResult',  data_accept)
        qrToken = acceptResult.get("qrToken")


        # # 检测过期状态
        # scanResult = self.postRequests('https://id.kuaishou.com/rest/c/infra/ks/qr/scanResult', data_accept)
        # print("=====检测过期状态=====:",scanResult)

        at_data = {
            'qrToken': qrToken,
            'sid': 'kuaishou.server.web'
        }


        #登陆接口
        result = self.postRequests('https://id.kuaishou.com/pass/kuaishou/login/qr/callback', at_data)

        print(result)




if __name__ == '__main__':


    """主逻辑"""
    spider = KsLogin()
    try:
        spider.main()
    except TimeoutError:
        # todo 未处理
        raise ValueError




上一篇:基于Python的二维码的在线编解码系统


下一篇:JOISC 2016 Day 1 棋盘游戏