alist谷歌云盘过期问题rclone
直接使用alist的接口获取的token超过7天过期,发现rclone不过期,写一个脚本rclone自动更新到alist中调用alist的api
直接上脚本
#!/usr/bin/env bash
# 调试开关
DEBUG=${DEBUG:-false}
# 根据调试开关设置 bash 选项
if [ "$DEBUG" = true ]; then
set -exu
else
set -eu
fi
# 全局变量
DOMAIN="al.myki.org"
# 调试输出函数
debug_log() {
if [ "$DEBUG" = true ]; then
echo "[DEBUG] $1" >&2
fi
}
# 函数:获取 rclone 配置
get_rclone_config() {
local config_name=$1
debug_log "Getting rclone config for $config_name"
local rclone_json=$(rclone config dump)
local refresh_token=$(echo "$rclone_json" | jq -r --arg key "$config_name" '.[$key].token | fromjson | .refresh_token')
local client_id=$(echo "$rclone_json" | jq -r --arg key "$config_name" '.[$key].client_id')
local client_secret=$(echo "$rclone_json" | jq -r --arg key "$config_name" '.[$key].client_secret')
echo "$refresh_token" "$client_id" "$client_secret"
}
# 函数:获取认证 token
get_auth_token() {
debug_log "Getting auth token"
local auth_response=$(curl -s --location "https://${DOMAIN}/api/auth/login" \
--header 'Content-Type: application/json' \
--data-raw '{
"username": "admin",
"password": "123456"
}')
echo "$auth_response" | jq -r '.data.token'
}
# 函数:更新存储
update_storage() {
local token=$1
local id=$2
local mount_path=$3
local refresh_token=$4
local client_id=$5
local client_secret=$6
local remark=$7
debug_log "Updating storage: $remark"
local url="https://${DOMAIN}/api/admin/storage/update"
local data="{
\"id\": $id,
\"mount_path\": \"$mount_path\",
\"order\": 0,
\"driver\": \"GoogleDrive\",
\"cache_expiration\": 30,
\"status\": \"work\",
\"addition\": \"{\\\"root_folder_id\\\":\\\"root\\\",\\\"refresh_token\\\":\\\"$refresh_token\\\",\\\"order_by\\\":\\\"\\\",\\\"order_direction\\\":\\\"\\\",\\\"client_id\\\":\\\"$client_id\\\",\\\"client_secret\\\":\\\"$client_secret\\\",\\\"chunk_size\\\":5}\",
\"remark\": \"$remark\",
\"disabled\": false,
\"enable_sign\": false,
\"order_by\": \"\",
\"order_direction\": \"\",
\"extract_folder\": \"\",
\"web_proxy\": false,
\"webdav_policy\": \"native_proxy\",
\"proxy_range\": false,
\"down_proxy_url\": \"\"
}"
local response=$(curl --location "$url" \
--header "Authorization: $token" \
--header 'Content-Type: application/json' \
--data "$data")
echo "$response"
}
# 主程序
main() {
debug_log "Starting main function"
# 获取配置
read -r REFRESH_TOKEN CLIENT_ID CLIENT_SECRET <<< $(get_rclone_config "替换成自己的rclone名字1")
read -r NEW_REFRESH_TOKEN NEW_CLIENT_ID NEW_CLIENT_SECRET <<< $(get_rclone_config "替换成自己的rclone名字2")
# 获取认证 token
TOKEN=$(get_auth_token)
# 检查 token 是否成功获取
if [ -z "$TOKEN" ]; then
echo "Failed to obtain authentication token"
exit 1
fi
# 定义存储配置
declare -A storages=(
["google_drive"]="5 /pbg $REFRESH_TOKEN $CLIENT_ID $CLIENT_SECRET desc1"
["21kixc"]="2 /pkg $NEW_REFRESH_TOKEN $NEW_CLIENT_ID $NEW_CLIENT_SECRET desc1"
)
# 更新所有存储
for storage in "${!storages[@]}"; do
debug_log "Processing storage: $storage"
read -r id mount_path refresh_token client_id client_secret remark <<< "${storages[$storage]}"
echo "Updating $storage storage:"
UPDATE_RESPONSE=$(update_storage "$TOKEN" "$id" "$mount_path" "$refresh_token" "$client_id" "$client_secret" "$remark")
echo "$UPDATE_RESPONSE" | jq '.'
echo
done
debug_log "Main function completed"
}
# 运行主程序
main
# 添加自动每天自动更新
# crontab -l 2>/dev/null | { cat; echo "30 02 * * * bash /opt/alist_refresh_token.sh"; } | crontab -
文章作者 🐳Myki
上次更新 2024-07-16