Files
ClaudeCode-Custom-Skills/skills/claw-setup/scripts/import-qwen-oauth.sh
Claude Code 9b46db629e feat: add auto token refresh for ALL platforms
New qwen-token-refresh.sh script provides automatic token refresh
for OpenClaw, NanoBot, PicoClaw, NanoClaw (ZeroClaw has native support).

Features:
- Check token status and expiry
- Auto-refresh when < 5 min remaining
- Background daemon mode (5 min intervals)
- Systemd service installation
- Updates both oauth_creds.json and .env file

Usage:
  ./scripts/qwen-token-refresh.sh --status   # Check status
  ./scripts/qwen-token-refresh.sh            # Refresh if needed
  ./scripts/qwen-token-refresh.sh --daemon   # Background daemon
  ./scripts/qwen-token-refresh.sh --install  # Systemd service

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-22 05:18:50 -05:00

209 lines
7.3 KiB
Bash
Executable File

#!/bin/bash
# Import FREE Qwen OAuth to any Claw platform
# Usage: source import-qwen-oauth.sh [platform]
#
# Platforms:
# zeroclaw - Native qwen-oauth provider (recommended, auto token refresh)
# picoclaw - OpenAI-compatible import
# openclaw - OpenAI-compatible import
# nanobot - OpenAI-compatible import
# nanoclaw - OpenAI-compatible import
#
# Requirements:
# - qwen-code CLI installed: npm install -g @qwen-code/qwen-code@latest
# - jq for JSON parsing
# - Authenticated: qwen --auth-type qwen-oauth -p "test"
set -e
CREDS_FILE="$HOME/.qwen/oauth_creds.json"
PLATFORM="${1:-zeroclaw}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "🦞 Claw Setup - Qwen OAuth Import"
echo "=================================="
echo ""
# Check for credentials file
if [ ! -f "$CREDS_FILE" ]; then
echo -e "${RED}Error: Qwen OAuth credentials not found${NC}"
echo "File expected at: $CREDS_FILE"
echo ""
echo "To authenticate, run:"
echo " npm install -g @qwen-code/qwen-code@latest"
echo " qwen --auth-type qwen-oauth -p 'test'"
echo ""
echo "This will open a browser for FREE OAuth login."
exit 1
fi
# Check for jq
if ! command -v jq &> /dev/null; then
echo -e "${YELLOW}Warning: jq not found, using grep/sed fallback${NC}"
QWEN_TOKEN=$(cat "$CREDS_FILE" | grep -o '"access_token"[[:space:]]*:[[:space:]]*"[^"]*"' | sed 's/.*"access_token"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
else
QWEN_TOKEN=$(cat "$CREDS_FILE" | jq -r '.access_token')
fi
if [ -z "$QWEN_TOKEN" ] || [ "$QWEN_TOKEN" = "null" ]; then
echo -e "${RED}Error: Could not extract access_token from credentials${NC}"
echo "Credentials file may be corrupted. Try re-authenticating:"
echo " qwen --auth-type qwen-oauth -p 'test'"
exit 1
fi
echo "Platform: $PLATFORM"
echo "Token: ${QWEN_TOKEN:0:20}..."
echo ""
case "$PLATFORM" in
zeroclaw)
# ZeroClaw uses native qwen-oauth provider
CONFIG_FILE="$HOME/.zeroclaw/config.toml"
if [ ! -f "$CONFIG_FILE" ]; then
echo -e "${YELLOW}ZeroClaw config not found, creating...${NC}"
mkdir -p "$HOME/.zeroclaw"
cat > "$CONFIG_FILE" << 'EOF'
default_provider = "qwen-oauth"
default_model = "qwen3-coder-plus"
default_temperature = 0.7
[autonomy]
level = "supervised"
workspace_only = true
[memory]
backend = "sqlite"
EOF
else
# Update existing config
sed -i 's/^default_provider = .*/default_provider = "qwen-oauth"/' "$CONFIG_FILE"
sed -i 's/^default_model = .*/default_model = "qwen3-coder-plus"/' "$CONFIG_FILE"
fi
echo -e "${GREEN}✅ ZeroClaw configured with native qwen-oauth provider${NC}"
echo ""
echo "ZeroClaw automatically:"
echo " • Reads ~/.qwen/oauth_creds.json"
echo " • Refreshes expired tokens"
echo " • Uses correct DashScope API endpoint"
echo ""
echo "Test with:"
echo " zeroclaw agent -m 'Hello!'"
;;
picoclaw)
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
echo -e "${GREEN}✅ PicoClaw environment configured${NC}"
echo ""
echo "Environment variables set:"
echo " OPENAI_API_KEY=${QWEN_TOKEN:0:20}..."
echo " OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1"
echo ""
echo "Test with:"
echo " picoclaw agent -m 'Hello!'"
;;
openclaw)
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
echo -e "${GREEN}✅ OpenClaw environment configured${NC}"
echo ""
echo "Environment variables set:"
echo " OPENAI_API_KEY=${QWEN_TOKEN:0:20}..."
echo " OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1"
echo ""
echo "Test with:"
echo " openclaw"
;;
nanobot)
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
export OPENAI_MODEL="qwen3-coder-plus"
echo -e "${GREEN}✅ NanoBot environment configured${NC}"
echo ""
echo "Environment variables set:"
echo " OPENAI_API_KEY=${QWEN_TOKEN:0:20}..."
echo " OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1"
echo " OPENAI_MODEL=qwen3-coder-plus"
echo ""
echo "Test with:"
echo " nanobot"
;;
nanoclaw)
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
echo -e "${GREEN}✅ NanoClaw environment configured${NC}"
echo ""
echo "Environment variables set:"
echo " OPENAI_API_KEY=${QWEN_TOKEN:0:20}..."
echo " OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1"
echo ""
echo "Test with:"
echo " nanoclaw"
;;
all)
# Configure all platforms
echo "Configuring all platforms..."
echo ""
# ZeroClaw
CONFIG_FILE="$HOME/.zeroclaw/config.toml"
if [ -f "$CONFIG_FILE" ]; then
sed -i 's/^default_provider = .*/default_provider = "qwen-oauth"/' "$CONFIG_FILE"
sed -i 's/^default_model = .*/default_model = "qwen3-coder-plus"/' "$CONFIG_FILE"
echo -e "${GREEN}✅ ZeroClaw${NC}"
fi
# Set environment for other platforms
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
echo -e "${GREEN}✅ PicoClaw, OpenClaw, NanoBot, NanoClaw${NC}"
echo ""
echo "Environment exported for current session."
echo "Add to ~/.bashrc for persistence:"
echo ""
echo 'export OPENAI_API_KEY="$(cat ~/.qwen/oauth_creds.json | jq -r '\''.access_token'\'')"' >> ~/.bashrc
echo 'export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"' >> ~/.bashrc
;;
*)
# Generic - set environment variables
export OPENAI_API_KEY="$QWEN_TOKEN"
export OPENAI_BASE_URL="https://dashscope.aliyuncs.com/compatible-mode/v1"
echo -e "${GREEN}✅ Generic environment configured for $PLATFORM${NC}"
echo ""
echo "Environment variables set:"
echo " OPENAI_API_KEY=${QWEN_TOKEN:0:20}..."
echo " OPENAI_BASE_URL=https://dashscope.aliyuncs.com/compatible-mode/v1"
;;
esac
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "FREE Qwen OAuth: 2,000 requests/day, 60 req/min"
echo "API Endpoint: https://dashscope.aliyuncs.com/compatible-mode/v1"
echo ""
echo "🔄 AUTO TOKEN REFRESH (for non-ZeroClaw platforms):"
echo " ./scripts/qwen-token-refresh.sh --status # Check status"
echo " ./scripts/qwen-token-refresh.sh # Refresh if needed"
echo " ./scripts/qwen-token-refresh.sh --daemon # Background daemon"
echo " ./scripts/qwen-token-refresh.sh --install # Systemd service"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"