自动刷新rt填入one-api/new-api

经过研究,它更新是通过一个 PUT 请求到 /api/channel/ 这个路径:

{
  "id": 29,
  "type": 1,
  "key": "AIzaSyCQtymKE6eN-5ftJ2fKAWVJrVwbnfkmXKk",
  "status": 2,
  "name": "Bing AI",
  "weight": 0,
  "created_time": 1706107012,
  "test_time": 1712784788,
  "response_time": 81,
  "base_url": "https://bingai.xxx.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"
  ],
  "is_edit": true
}

这个key就是前端那个key,这里换成at应该就行。id就是渠道id,这个可以找到,不过我还没发现它怎么验证请求的,似乎没写在header里。待我研究研究,代码我稍后实现一下

1 个赞

抓包好了,登入的时候会返回一个 session,后面的请求里,cookie 会附上这个 session

1 个赞

哈哈那就是了我就是inker里面发的

用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.")

前一阵也跟上了始皇的节奏,改了改newapi,支持填入rt自动获取和刷新at,自定义请求头,还加了个支持走网络代理

真是本尊

Error: Failed to update one-api channel info.

不行

二开了new-api吗?仓库地址来一个

暂时没放出来,提交记录里面有点隐私内容,周末有空整理下,再放出来,只能说是能用,优雅指定是不太优雅 :joy:

没事,能用就是优雅 :face_with_peeking_eye:

坛除我佬

这跟用始皇的 chat2api 有啥区别?

不错的代码

如果始皇那边支持rt2api就好了

思路复杂了,没必要那么麻烦呀:sweat_smile: