get_token.sh
1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# get_feishu_token.sh - 获取飞书 tenant_access_token
set -e
APP_ID="${FEISHU_APP_ID:-cli_a91b01debab81cd1}"
APP_SECRET="${FEISHU_APP_SECRET:-}"
if [ -z "$APP_SECRET" ]; then
# 尝试从 .env 文件读取
if [ -f "$HOME/.hermes/.env" ]; then
APP_SECRET=$(grep "FEISHU_APP_SECRET" "$HOME/.hermes/.env" | cut -d'=' -f2)
fi
fi
if [ -z "$APP_SECRET" ]; then
echo "Error: FEISHU_APP_SECRET not set"
echo "Please set it in ~/.hermes/.env or environment variable"
exit 1
fi
RESPONSE=$(curl -s -X POST \
https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal \
-H "Content-Type: application/json" \
-d "{\"app_id\":\"$APP_ID\",\"app_secret\":\"$APP_SECRET\"}")
CODE=$(echo "$RESPONSE" | grep -o '"code":[0-9]*' | cut -d':' -f2)
if [ "$CODE" != "0" ]; then
echo "Error: Failed to get token"
echo "$RESPONSE"
exit 1
fi
TOKEN=$(echo "$RESPONSE" | grep -o '"tenant_access_token":"[^"]*"' | cut -d'"' -f4)
EXPIRE=$(echo "$RESPONSE" | grep -o '"expire":[0-9]*' | cut -d':' -f2)
echo "Token: $TOKEN"
echo "Expire: $EXPIRE seconds ($(($EXPIRE / 60)) minutes)"
# 保存 token 到临时文件
mkdir -p /sdcard/feishu
echo "$TOKEN" > /sdcard/feishu/token
echo "Token saved to /sdcard/feishu/token"