(python)通过refresh_token获取access_token,通过access_token获取share_token,并生成txt文件保存在指定路径

前人栽树,后人乘凉
感谢

直接贴上代码
通过refresh_token获取access_token(python)

import os
import requests


def get_access_token(refresh_token):
    url = "https://token.oaifree.com/api/auth/refresh"
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    }
    data = f'refresh_token={refresh_token}'

    response = requests.post(url, headers=headers, data=data)

    if response.status_code == 200:
        result = response.json()
        return result.get('access_token', 'No access token found in response')
    else:
        return f"Error: {response.status_code}, {response.text}"


# 使用提供的refresh_token
refresh_token = "你的refresh_token"#需要修改
access_token = get_access_token(refresh_token)
print(f'Access Token: {access_token}')

# 文件路径
file_path = 'C:/Users/杰/Desktop/new_token/access_token/access_token.txt'#需要修改为你自己的地址,不一定需要创建在桌面
directory = os.path.dirname(file_path)

# 创建目录(如果不存在)
if not os.path.exists(directory):
    os.makedirs(directory)

# 将access_token写入文件
with open(file_path, 'w', encoding='utf-8') as file:
    file.write(access_token)

通过access_token获取refresh_token(python)

import requests
import os


# 生成 share token
def generate_share_token(access_token, unique_name):
    url = "https://chat.oaifree.com/token/register"
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
    data = {
        'unique_name': unique_name,
        'access_token': access_token,
        'site_limit': '',  # 对网站的限制(可以修改)
        'expires_in': 0,
        'show_conversations': 'true',  # 是否开启会话隔离(可以修改)
        "gpt35_limit": '-1',  # gpt35次数限制(可以修改)
        "gpt4_limit": '-1',  # gpt4次数限制(可以修改)
        "reset_limit": 'false',  # 是否重置次数(可以修改)
        "temporary_chat": 'true'  # 是否开启临时会话(可以修改)
    }
    response = requests.post(url, headers=headers, data=data)
    return response.text


# 读取本地文件中的 access token
def read_access_token(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        access_token = file.read().strip()
    return access_token


# 检查并更新或追加新的 share token
def update_share_token(file_path, unique_name, new_share_token):
    new_share_token_lines = new_share_token.replace(',', ',\n')

    if not os.path.exists(file_path):
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(f"{unique_name}:\n{new_share_token_lines}\n\n")
    else:
        with open(file_path, 'r', encoding='utf-8') as file:
            lines = file.readlines()

        updated = False
        with open(file_path, 'w', encoding='utf-8') as file:
            for i, line in enumerate(lines):
                if line.startswith(f"{unique_name}:"):
                    file.write(f"{unique_name}:\n{new_share_token_lines}\n\n")
                    updated = True
                    # Skip next line(s) as we're replacing the token
                    while i + 1 < len(lines) and lines[i + 1].strip() != '':
                        i += 1
                else:
                    file.write(line)
            if not updated:
                if lines and lines[-1].strip() != '':
                    file.write("\n")
                file.write(f"{unique_name}:\n{new_share_token_lines}\n\n")


# 文件路径
access_token_file_path = r'C:\Users\杰\Desktop\new_token\access_token\access_token.txt'#填写access_token.txt路径#需要修改为你自己的地址,不一定需要创建在桌面(可以修改)
share_token_file_path = r'C:\Users\杰\Desktop\new_token\share_token\share_token.txt'#填写share_token.txt路径#需要修改为你自己的地址,不一定需要创建在桌面(可以修改)
unique_name = 'z1'#用户名,隔离对话(可以修改)

# 使用读取的 access token 生成并打印 share token
access_token = read_access_token(access_token_file_path)
share_token_response = generate_share_token(access_token, unique_name)
print("Share Token Response:", share_token_response)

# 更新或追加 share token 到文件
update_share_token(share_token_file_path, unique_name, share_token_response)

可以将生成的记录保存在txt中


11 个赞

重复发帖!

啊?!之前发过一遍但是在我账号里没找到话题,我就又发了一遍

感谢

感恩

第二段代码说错了,是通过access token 生成share token

挺好,这样省时间了。