用http请求更新one-api的实现,有谁测试一下?
import os
import requests
import datetime
import json
import urllib.parse
# Variables
## Log
log_path = "/path/to/log"
## OpenAI
refresh_token = "xxxxxxxxxxx"
## One API
one_api_url = "https://one-api.jamesflare.com"
one_api_username = "xxxx"
one_api_password = "xxxx"
channel_id = "xx"
## Telegram
enable_tg = False
# Define log file
log_file = os.path.join(log_path, 'script_log_' + datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + '.log')
# Official website to get access_token
response = requests.post(
"https://auth0.openai.com/oauth/token",
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
})
)
result1 = response.text
# Extract access_token from result1
result1_json = json.loads(result1)
session_token = result1_json.get('session_token')
access_token = result1_json.get('access_token')
print(session_token)
if access_token is None:
with open(log_file, 'a') as f:
f.write("Error: Failed to get access_token.\n")
print("Error: Failed to get access_token.")
exit(1)
# Update one-api channel info
## Get one-api session cookies
one_api_response = requests.post(
one_api_url + "/api/user/login",
headers={"Content-Type": "application/json"},
data=json.dumps({
"username": one_api_username,
"password": one_api_password
})
)
one_api_session_cookies = one_api_response.cookies.get('session')
## Get existing channel info
"""response:
{
"data": {
"id": 29,
"type": 1,
"key": "",
"status": 2,
"name": "Bing AI",
"weight": 0,
"created_time": 1706107012,
"test_time": 1712784788,
"response_time": 81,
"base_url": "https://xxx.jamesflare.com",
"other": "",
"balance": 0,
"balance_updated_time": 0,
"models": "BingAI-Creative-Online,BingAI-Creative-Offline",
"group": "default,vip,svip",
"used_quota": 19520,
"model_mapping": "{\n \"BingAI-Creative-Online\": \"Creative-g4t\",\n \"BingAI-Creative-Offline\": \"Creative-g4t-offline\"\n}",
"priority": 0,
"config": ""
},
"message": "",
"success": true
}
"""
one_api_response = requests.get(
one_api_url + "/api/channel/" + channel_id,
cookies={"session": one_api_session_cookies}
)
if one_api_response.json().get('success') != True:
with open(log_file, 'a') as f:
f.write("Error: Failed to get one-api channel info.\n")
print("Error: Failed to get one-api channel info.")
sys.exit(1)
## Update channel info
"""request:
{
"id": 29,
"type": 1,
"key": "114514",
"status": 2,
"name": "Bing AI",
"weight": 0,
"created_time": 1706107012,
"test_time": 1712784788,
"response_time": 81,
"base_url": "https://xxx.jamesflare.com",
"other": "",
"balance": 0,
"balance_updated_time": 0,
"models": "BingAI-Creative-Online,BingAI-Creative-Offline",
"group": "default",
"used_quota": 19520,
"model_mapping": "{\n \"BingAI-Creative-Online\": \"Creative-g4t\",\n \"BingAI-Creative-Offline\": \"Creative-g4t-offline\"\n}",
"priority": 0,
"config": "",
"groups": [
"default",
"vip",
"svip"
],
"is_edit": true
}
"""
updater_json = one_api_response.json().get('data')
updater_json['group'] = one_api_response.json().get('data').get('group').split(',')
updater_json['key'] = access_token
updater_json['is_edit'] = True
update_response = requests.post(
one_api_url + "/api/channel/",
headers={"Content-Type": "application/json"},
cookies={"session": one_api_session_cookies},
data=json.dumps(updater_json)
)
if update_response.json().get('success') != True:
with open(log_file, 'a') as f:
f.write("Error: Failed to update one-api channel info.\n")
print("Error: Failed to update one-api channel info.")
sys.exit(1)
# Telegram notification
if enable_tg == True:
bot_token = "xxxxxxx"
chat_id = "xxxxxx"
message = "*access token获取成功*\n" + access_token
# URL encode message
encoded_message = urllib.parse.quote(message)
# Send message
response = requests.post(
"https://api.telegram.org/bot{}/sendMessage".format(bot_token),
data={
'chat_id': chat_id,
'text': encoded_message
}
)
with open(log_file, 'a') as f:
f.write("Script executed successfully.\n")
print("Script executed successfully.")