出处:不记得是哪位分享的了
需要自行修改rt刷新at的部分,或者使用官方
https://chat.openai.com/api/auth/session
来刷新
#!/bin/bash
#sudo apt-get update
#sudo apt-get install jq 安装jq组件
#sqlite3 /真实路径/one-api.db "SELECT key FROM channels;" 查询当前access token
#0 5 * * 1 /真实路径/token_oneapi_test.sh 定时每周一5点执行
# 定义日志文件
LOGFILE="$(pwd)/script_log_$(date +%Y%m%d_%H%M%S).log"
# 官网获取access_token,来源始皇
result1=$(curl -s -X POST "https://auth0.openai.com/oauth/token" \
-H "Content-Type: application/json" \
-d '{
"redirect_uri": "com.openai.chat://auth0.openai.com/ios/com.openai.chat/callback",
"grant_type": "refresh_token",
"client_id": "pdlLIX2Y72MIl2rhLhTE9VV9bN905kBh",
"refresh_token": "xxxxxxxxxxx"
}') &>> $LOGFILE
# 从result1中提取access_token
session_token=$(echo "$result1" | jq -r '.session_token')
access_token=$(echo "$result1" | jq -r '.access_token')
echo $session_token
# 检查access_token是否为空
if [ -z "$access_token" ]; then
echo "Error: Failed to obtain access token." >> $LOGFILE
exit 1
fi
# 更新序号为1的数据库中的token,ID在one-api的渠道位置查看
sqlite3 /home/ubuntu/one-api/data/one-api.db "UPDATE channels SET key = \"$access_token\" WHERE id = 58;" &>> $LOGFILE
# 检查SQLite命令执行状态
if [ $? -ne 0 ]; then
echo "Error: Failed to update database." >> $LOGFILE
exit 1
fi
# 微信通知
#pushURL="http://www.pushplus.plus/send"
#title="update%20access%20token"
#content=$(echo -e "$access_token" | jq -sRr @uri)
#curl -s "$pushURL?token=替换我发通知&title=$title&content=$content&template=html" >> $LOGFILE
# telegram通知
bot_token="xxxxxxx"
chat_id="xxxxxx"
message="*access token获取成功*\n$access_token"
# URL编码消息
encoded_message=$(echo -e "$message" | jq -sRr @uri)
# 发送消息
curl -s -X POST "https://api.telegram.org/bot$bot_token/sendMessage" -d chat_id="$chat_id" -d text="$encoded_message"
# 检查CURL命令执行状态
if [ $? -ne 0 ]; then
echo "Error: Failed to send WeChat notification." >> $LOGFILE
exit 1
fi
echo "Script executed successfully." >> $LOGFILE