ChatGPT3.5免登录调用API

基于官方提供的免登录web页面接口,自行添加JW代理,一些节点可能无法使用。

目前没有处理人机验证,如遇验证需要点击或换节点。

file

代码如下:

# coding=utf-8
import time
from curl_cffi import requests
import uuid
import base64
from hashlib import sha3_512
import json
import random
from datetime import datetime, timedelta, timezone

class ProofWorker:
    def __init__(self, difficulty=None, required=False, seed=None):
        self.difficulty = difficulty
        self.required = required
        self.seed = seed
        self.proof_token_prefix = "gAAAAABwQ8Lk5FbGpA2NcR9dShT6gYjU7VxZ4D"

    def get_parse_time(self):
        now = datetime.now()
        tz = timezone(timedelta(hours=8))
        now = now.astimezone(tz)
        time_format = "%a %b %d %Y %H:%M:%S"
        return now.strftime(time_format) + " GMT+0800 (中国标准时间)"

    def get_config(self,OPENAI_GET_HEADERS):
        cores = [8, 12, 16, 24]
        core = random.choice(cores)
        screens = [3000, 4000, 6000]
        screen = random.choice(screens)
        return [
            str(core) + str(screen),
            self.get_parse_time(),
            4294705152,
            0,
            OPENAI_GET_HEADERS,
        ]

    def calc_proof_token(self, seed: str, difficulty: str,OPENAI_GET_HEADERS:str):
        config = self.get_config(OPENAI_GET_HEADERS)
        diff_len = len(difficulty) // 2
        for i in range(100000):
            config[3] = i
            json_str = json.dumps(config)
            base = base64.b64encode(json_str.encode()).decode()
            hasher = sha3_512()
            hasher.update((seed + base).encode())
            hash = hasher.digest().hex()
            if hash[:diff_len] <= difficulty:
                return "gAAAAAB" + base
        self.proof_token = (
            self.proof_token_prefix + base64.b64encode(seed.encode()).decode()
        )
        return self.proof_token

PW = ProofWorker()

parts = "who are you"

proxies = {"https": "127.0.0.1:15732"}

headers = {
    "Accept": "text/event-stream",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Content-Type": "application/json",
    "Origin": "https://chat.openai.com",
    "Referer": "https://chat.openai.com/",
    "Sec-Ch-Ua": "\"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"",
    "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/122.0.0.0 Safari/537.36",
    "Oai-Language": "en-US",
}
sess = requests.Session()
sess.get("https://chat.openai.com/?model=text-davinci-002-render-sha", headers=headers,proxies=proxies)

requirements=sess.post("https://chat.openai.com/backend-anon/sentinel/chat-requirements",json={}, headers=headers,proxies=proxies).json()
token = requirements['token']
proofofwork = requirements['proofofwork']
proof_token = PW.calc_proof_token(proofofwork['seed'],proofofwork['difficulty'],headers['User-Agent'])
headers['Openai-Sentinel-Chat-Requirements-Token'] = token
headers['Openai-Sentinel-Proof-Token'] = proof_token

data = {
        "action": "next",
        "messages": [{
            "id": str(uuid.uuid4()),
            "author": {"role": "user"},
            "content": {"content_type": "text", "parts": [parts]}, "metadata": {}
        }],
        "parent_message_id": str(uuid.uuid4()), "model": "text-davinci-002-render-sha",
        "timezone_offset_min": -480, "suggestions": [],
        "history_and_training_disabled": False,
        "conversation_mode": {"kind": "primary_assistant"}, "force_paragen": False, "force_paragen_model_slug": "",
        "force_nulligen": False, "force_rate_limit": False,
        "websocket_request_id": str(uuid.uuid4())
}
response = sess.post('https://chat.openai.com/backend-anon/conversation', headers=headers, json=data,proxies=proxies)
print(response.text)

file

点赞
  1. 枫原万叶说道:
    Google Chrome Mac OS X 10.15.7
    https://chat.openai.com/backend-anon/sentinel/chat-requirements 这个接口好想要带 cookie 了
  2. 枫原万叶说道:
    Google Chrome Mac OS X 10.15.7
    sess.get("https://chat.openai.com/?model=text-davinci-002-render-sha", headers=headers,proxies=proxies) 这个是在 js 中执行的么,调试抓包没有看到这个接口信息
    1. Lx Lx说道:
      Google Chrome Windows 10
      第一次访问 openai.com 页面时的
  3. 五文钱说道:
    Google Chrome Windows 10
    我这里获得token部门一直报错未认证,不知道是不是代理问题了
    1. Lx Lx说道:
      Google Chrome Windows 10
      你浏览器打开看看,应该出验证了
      1. 五文钱说道:
        Google Chrome Windows 10
        好像换了个接口好了,但这玩意也有个问题。他为啥是一个字一个字返回的结果,好长
        1. Lx Lx说道:
          Google Chrome Windows 10
          返回是这样的

发表回复