tgs login#

对 Telegram 进行身份验证。会话保存在本地,后续所有命令自动复用。

用法#

tgs login [flags]

参数#

FlagShortDefaultDescription
--type-TdesktopAuth method: desktop, code, or qr
--desktop-dir-d(auto-detect)Path to Telegram Desktop tdata directory
--passcodeTelegram Desktop local passcode
--phonePhone number for code method
--codeVerification code (for non-interactive login)
--password2FA cloud password
--profile-p(resolved)Profile name to authenticate into

未设置 --profile 时的配置文件解析顺序:TGS_PROFILE 环境变量 → .tgs.yaml 文件 → "default"

示例#

# Import session from Telegram Desktop (default)
tgs login

# Custom tdata path
tgs login --desktop-dir ~/AppData/Roaming/Telegram\ Desktop/tdata

# Passcode-protected Telegram Desktop
tgs login --passcode mysecret

# Phone + verification code (interactive)
tgs login --type code

# Phone + code with phone pre-filled
tgs login --type code --phone +1234567890

# Non-interactive two-step login (for AI agents / automation)
# Step 1: send the verification code
tgs login --type code --phone +1234567890
# Step 2: complete login with the code
tgs login --type code --phone +1234567890 --code 12345

# Non-interactive login with 2FA
tgs login --type code --phone +1234567890 --code 12345 --password mysecret

# QR code login
tgs login --type qr

# Login to a specific profile
tgs login --profile work

# Login to a profile with a specific method
tgs login --type code --profile work --phone +0987654321

认证方式#

MethodDescription
desktopImport session from Telegram Desktop. No phone or code needed.
codePhone number + SMS/Telegram verification code. Supports 2FA. Can be fully non-interactive with --phone, --code, and --password flags.
qrDisplay QR code in terminal. Scan with Telegram on another device. Accounts with 2FA enabled cannot use QR login — use code with --password instead.

输出#

登录成功时:

JSON(默认):

{"profile":"default","status":"logged_in","user":{"id":261054642,"phone":"+79001234567","username":"alice","first_name":"Alice","last_name":"Doe"}}

文本(--output text):

Logged in as Alice Doe (id: 261054642, profile: default)

在两步 code 流程中,第一次调用(不带 --code)会发送验证码:

{"profile":"default","status":"code_sent"}
Verification code sent (profile: default). Re-run with --code to complete login.

失败时命令以非零退出码结束并将错误写入 stderr。常见原因:验证码无效、QR 令牌过期、2FA 密码错误,或会话已存在(需先执行 tgs logout)。

另请参阅#