File size: 2,469 Bytes
75eef29
 
f7f2cd4
75eef29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import requests
import json
import os

# 定义常量
TOKEN_URL_NEO = 'https://token.oaifree.com/api/auth/refresh'
TOKEN_URL_OAI = 'https://auth0.openai.com/oauth/token'

def get_access_token(refresh_token, mode):
    """

    获取 access token 的函数

    

    :param refresh_token: refresh token

    :param mode: 'neo' 或 'oai'

    :return: access token 或错误信息

    """
    url = TOKEN_URL_OAI if mode == 'oai' else TOKEN_URL_NEO
    
    if mode == 'neo':
        headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}
        data = f'refresh_token={refresh_token}'
    else:
        headers = {'Content-Type': 'application/json'}
        data = json.dumps({
            'redirect_uri': "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
            'grant_type': "refresh_token",
            'client_id': "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
            'refresh_token': refresh_token
        })
    
    try:
        response = requests.post(url, headers=headers, data=data)
        result = response.json()
        
        if 'access_token' in result:
            return result['access_token']
        else:
            return f"获取失败:{result.get('detail', '未知错误')}"
    
    except requests.RequestException as e:
        return f"请求错误:{str(e)}"

def batch_get_access_tokens(input_file='refresh_token.txt', output_file='access_token.txt'):
    """

    批量获取 access tokens

    

    :param input_file: 包含 refresh tokens 的输入文件

    :param output_file: 写入 access tokens 的输出文件

    """
    # 从环境变量读取 refresh tokens
    refresh_tokens = os.getenv('REFRESH_TOKENS', '')
    if refresh_tokens:
        refresh_tokens = refresh_tokens.split(',')
    else:
        # 如果没有环境变量,使用默认文件读取
        with open(input_file, 'r') as f_in:
            refresh_tokens = [line.strip() for line in f_in]

    with open(output_file, 'w') as f_out:
        for refresh_token in refresh_tokens:
            # 默认使用 'neo' 模式,可以根据需要修改
            access_token = get_access_token(refresh_token, mode='oai')
            
            # 写入 access token
            f_out.write(f"{access_token}\n")
    
    print(f"Access tokens 已写入 {output_file}")

# 主程序入口
if __name__ == "__main__":
    batch_get_access_tokens()