Download at.py from deeme/at: direct link, hf CLI and curl.
- Browser
- Download file 2.47 kB
-
https://huggingface.co/spaces/deeme/at/resolve/main/at.py
- Command line
-
hf download hf://spaces/deeme/at/at.py
-
curl -L -o at.py https://huggingface.co/spaces/deeme/at/resolve/main/at.py
2.47 kB
| 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() | |