douyin-cli 2026.8.23

A Rust CLI for Douyin OpenAPI and web workflows
Documentation
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use std::collections::HashMap;
use std::io::{self, Write};

use clap::{Args, Subcommand, ValueEnum};
use serde_json::{json, Map, Value};

use crate::err;
use crate::openapi::{im_message_body, OpenApiClient, RequestSpec};
use crate::settings;

#[derive(Debug, Args)]
pub struct ApiArgs {
    #[command(subcommand)]
    command: ApiCommand,
}

#[derive(Debug, Subcommand)]
enum ApiCommand {
    /// 获取 client_token
    ClientToken {
        #[arg(long, env = "DOUYIN_CLIENT_KEY")]
        client_key: String,
        #[arg(long, env = "DOUYIN_CLIENT_SECRET")]
        client_secret: String,
    },
    /// 生成官方 OAuth 授权链接
    AuthorizeUrl {
        #[arg(long, env = "DOUYIN_CLIENT_KEY")]
        client_key: String,
        #[arg(long)]
        redirect_uri: String,
        #[arg(long, required = true)]
        scope: Vec<String>,
        #[arg(long)]
        state: Option<String>,
    },
    /// 用 OAuth code 换取 access_token
    AccessToken {
        #[arg(long, env = "DOUYIN_CLIENT_KEY")]
        client_key: String,
        #[arg(long, env = "DOUYIN_CLIENT_SECRET")]
        client_secret: String,
        #[arg(long)]
        code: String,
    },
    /// 刷新官方 access_token
    RefreshToken {
        #[arg(long, env = "DOUYIN_CLIENT_KEY")]
        client_key: String,
        #[arg(long)]
        refresh_token: String,
    },
    /// 续期官方 refresh_token
    RenewRefreshToken {
        #[arg(long, env = "DOUYIN_CLIENT_KEY")]
        client_key: String,
        #[arg(long)]
        refresh_token: String,
    },
    /// 获取官方授权用户信息
    Userinfo(AuthOptions),
    /// 调用官方接口获取视频评论列表
    CommentList {
        #[command(flatten)]
        auth: AuthOptions,
        #[arg(long)]
        item_id: String,
        #[arg(long, default_value_t = 0)]
        cursor: u64,
        #[arg(long, default_value_t = 20, value_parser = clap::value_parser!(u32).range(1..=20))]
        count: u32,
        /// 0=综合排序,1=最多点赞,2=最新发布
        #[arg(long, value_parser = clap::value_parser!(u8).range(0..=2))]
        sort_type: Option<u8>,
    },
    /// 调用官方接口获取评论回复列表
    CommentReplies {
        #[command(flatten)]
        auth: AuthOptions,
        #[arg(long)]
        item_id: String,
        #[arg(long)]
        comment_id: String,
        #[arg(long, default_value_t = 0)]
        cursor: u64,
        #[arg(long, default_value_t = 20, value_parser = clap::value_parser!(u32).range(1..=20))]
        count: u32,
        /// 0=综合排序,1=最多点赞,2=最新发布
        #[arg(long, value_parser = clap::value_parser!(u8).range(0..=2))]
        sort_type: Option<u8>,
    },
    /// 调用官方接口回复视频评论
    CommentReply {
        #[command(flatten)]
        auth: AuthOptions,
        #[arg(long)]
        item_id: String,
        #[arg(long)]
        comment_id: Option<String>,
        #[arg(long)]
        content: String,
        #[arg(long)]
        yes: bool,
    },
    /// 通过官方私信接口回复或首次进入会话
    ImMessageSend {
        #[command(flatten)]
        auth: AuthOptions,
        #[arg(long)]
        to_user_id: String,
        /// 私信场景;reply/enter 也可作为别名
        #[arg(long, value_enum, default_value_t = ImScene::Reply)]
        scene: ImScene,
        /// 回调事件中的消息 ID
        #[arg(long)]
        msg_id: String,
        /// 回调事件中的会话 ID
        #[arg(long)]
        conversation_id: String,
        #[arg(long, value_enum, default_value_t = MessageType::Text)]
        message_type: MessageType,
        #[arg(long)]
        text: Option<String>,
        #[arg(long)]
        media_id: Option<String>,
        #[arg(long)]
        item_id: Option<String>,
        #[arg(long)]
        yes: bool,
    },
    /// 调用任意官方 OpenAPI 路径
    Request {
        method: String,
        path: String,
        #[arg(long, env = "DOUYIN_ACCESS_TOKEN")]
        token: Option<String>,
        #[arg(long = "param")]
        params: Vec<String>,
        #[arg(long = "json")]
        json_text: Option<String>,
        #[arg(long = "form")]
        forms: Vec<String>,
        #[arg(long = "header")]
        headers: Vec<String>,
    },
}

#[derive(Debug, Args)]
struct AuthOptions {
    /// 默认读取已保存 token
    #[arg(long, env = "DOUYIN_ACCESS_TOKEN")]
    token: Option<String>,
    /// 默认读取已保存 open_id
    #[arg(long)]
    open_id: Option<String>,
}

#[derive(Clone, Debug, ValueEnum)]
enum MessageType {
    Text,
    Image,
    Video,
}

#[derive(Clone, Debug, ValueEnum)]
enum ImScene {
    #[value(name = "im-reply-msg", alias = "reply")]
    Reply,
    #[value(name = "im-enter-direct-msg", alias = "enter")]
    Enter,
}

impl ImScene {
    fn as_str(&self) -> &'static str {
        match self {
            Self::Reply => "im_reply_msg",
            Self::Enter => "im_enter_direct_msg",
        }
    }
}

pub fn run(args: ApiArgs) -> Result<(), String> {
    let client = OpenApiClient::new()?;
    let response = match args.command {
        ApiCommand::ClientToken {
            client_key,
            client_secret,
        } => client.client_token(&client_key, &client_secret)?,
        ApiCommand::AuthorizeUrl {
            client_key,
            redirect_uri,
            scope,
            state,
        } => {
            println!(
                "{}",
                client.authorize_url(&client_key, &redirect_uri, &scope, state.as_deref())?
            );
            return Ok(());
        }
        ApiCommand::AccessToken {
            client_key,
            client_secret,
            code,
        } => client.access_token(&client_key, &client_secret, &code)?,
        ApiCommand::RefreshToken {
            client_key,
            refresh_token,
        } => client.refresh_token(&client_key, &refresh_token)?,
        ApiCommand::RenewRefreshToken {
            client_key,
            refresh_token,
        } => client.renew_refresh_token(&client_key, &refresh_token)?,
        ApiCommand::Userinfo(auth) => {
            let (token, open_id) = resolve_auth(auth)?;
            client.request(RequestSpec {
                method: "GET",
                path: "/oauth/userinfo/",
                token: Some(&token),
                params: Some(HashMap::from([("open_id".to_owned(), open_id)])),
                auth_required: true,
                ..RequestSpec::default()
            })?
        }
        ApiCommand::CommentList {
            auth,
            item_id,
            cursor,
            count,
            sort_type,
        } => {
            let (token, open_id) = resolve_auth(auth)?;
            let mut params = HashMap::from([
                ("open_id".to_owned(), open_id),
                ("item_id".to_owned(), item_id),
                ("cursor".to_owned(), cursor.to_string()),
                ("count".to_owned(), count.to_string()),
            ]);
            if let Some(sort_type) = sort_type {
                params.insert("sort_type".to_owned(), sort_type.to_string());
            }
            client.request(RequestSpec {
                method: "GET",
                path: "/item/comment/list/",
                token: Some(&token),
                params: Some(params),
                auth_required: true,
                ..RequestSpec::default()
            })?
        }
        ApiCommand::CommentReplies {
            auth,
            item_id,
            comment_id,
            cursor,
            count,
            sort_type,
        } => {
            let (token, open_id) = resolve_auth(auth)?;
            let mut params = HashMap::from([
                ("open_id".to_owned(), open_id),
                ("item_id".to_owned(), item_id),
                ("comment_id".to_owned(), comment_id),
                ("cursor".to_owned(), cursor.to_string()),
                ("count".to_owned(), count.to_string()),
            ]);
            if let Some(sort_type) = sort_type {
                params.insert("sort_type".to_owned(), sort_type.to_string());
            }
            client.request(RequestSpec {
                method: "GET",
                path: "/item/comment/reply/list/",
                token: Some(&token),
                params: Some(params),
                auth_required: true,
                ..RequestSpec::default()
            })?
        }
        ApiCommand::CommentReply {
            auth,
            item_id,
            comment_id,
            content,
            yes,
        } => {
            let (token, open_id) = resolve_auth(auth)?;
            validate_text(&content, "评论内容", 100, false)?;
            confirm_write("将通过官方 OpenAPI 发送评论回复,是否继续?", yes)?;
            let mut body = Map::from_iter([
                ("item_id".to_owned(), json!(item_id)),
                ("content".to_owned(), json!(content)),
            ]);
            if let Some(comment_id) = comment_id {
                body.insert("comment_id".to_owned(), json!(comment_id));
            }
            client.request(RequestSpec {
                method: "POST",
                path: "/item/comment/reply/",
                token: Some(&token),
                params: Some(HashMap::from([("open_id".to_owned(), open_id)])),
                json_body: Some(Value::Object(body)),
                auth_required: true,
                ..RequestSpec::default()
            })?
        }
        ApiCommand::ImMessageSend {
            auth,
            to_user_id,
            scene,
            msg_id,
            conversation_id,
            message_type,
            text,
            media_id,
            item_id,
            yes,
        } => {
            let (token, open_id) = resolve_auth(auth)?;
            let content = message_content(&message_type, text, media_id, item_id)?;
            confirm_write("将通过官方 OpenAPI 发送私信消息,是否继续?", yes)?;
            client.request(RequestSpec {
                method: "POST",
                path: "/im/send/msg/",
                token: Some(&token),
                params: Some(HashMap::from([("open_id".to_owned(), open_id)])),
                json_body: Some(im_message_body(
                    &to_user_id,
                    scene.as_str(),
                    &msg_id,
                    &conversation_id,
                    content,
                )),
                auth_required: true,
                ..RequestSpec::default()
            })?
        }
        ApiCommand::Request {
            method,
            path,
            token,
            params,
            json_text,
            forms,
            headers,
        } => {
            let data = settings::load().map_err(err)?;
            let saved = settings::openapi(&data);
            let token = token.or_else(|| saved_string(&saved, "accessToken"));
            client.request(RequestSpec {
                method: &method,
                path: &path,
                token: token.as_deref(),
                params: parse_key_values(params)?,
                json_body: parse_json(json_text)?,
                form: parse_key_values(forms)?,
                headers: parse_key_values(headers)?,
                auth_required: true,
            })?
        }
    };
    print_json(&response)
}

fn resolve_auth(options: AuthOptions) -> Result<(String, String), String> {
    let data = settings::load().map_err(err)?;
    let saved = settings::openapi(&data);
    let token = options
        .token
        .or_else(|| saved_string(&saved, "accessToken"))
        .ok_or_else(|| "缺少 access_token,请先运行 douyin auth login".to_owned())?;
    let open_id = options
        .open_id
        .or_else(|| saved_string(&saved, "openId"))
        .ok_or_else(|| "缺少 open_id,请先运行 douyin auth login".to_owned())?;
    Ok((token, open_id))
}

fn saved_string(values: &Map<String, Value>, key: &str) -> Option<String> {
    values
        .get(key)
        .and_then(Value::as_str)
        .filter(|value| !value.is_empty())
        .map(str::to_owned)
}

fn message_content(
    message_type: &MessageType,
    text: Option<String>,
    media_id: Option<String>,
    item_id: Option<String>,
) -> Result<Value, String> {
    let (code, kind, key, value, error) = match message_type {
        MessageType::Text => (1, "text", "text", text, "message-type=text 需要 --text"),
        MessageType::Image => (
            2,
            "image",
            "media_id",
            media_id,
            "message-type=image 需要 --media-id",
        ),
        MessageType::Video => (
            3,
            "video",
            "item_id",
            item_id,
            "message-type=video 需要 --item-id",
        ),
    };
    let value = value
        .filter(|value| !value.trim().is_empty())
        .ok_or(error)?;
    if matches!(message_type, MessageType::Text) {
        validate_text(&value, "私信文本", 1_000, true)?;
    }
    let payload = Value::Object(Map::from_iter([(key.to_owned(), json!(value))]));
    Ok(Value::Object(Map::from_iter([
        ("msg_type".to_owned(), json!(code)),
        (kind.to_owned(), payload),
    ])))
}

fn validate_text(
    value: &str,
    name: &str,
    max_chars: usize,
    forbid_links: bool,
) -> Result<(), String> {
    let length = value.chars().count();
    if length == 0 {
        return Err(format!("{name}不能为空"));
    }
    if length > max_chars {
        return Err(format!(
            "{name}不能超过 {max_chars} 个字符(当前 {length}"
        ));
    }
    if forbid_links && (value.contains("http://") || value.contains("https://")) {
        return Err(format!("{name}不能包含链接"));
    }
    Ok(())
}

fn parse_key_values(values: Vec<String>) -> Result<Option<HashMap<String, String>>, String> {
    if values.is_empty() {
        return Ok(None);
    }
    values
        .into_iter()
        .map(|value| {
            let (key, value) = value
                .split_once('=')
                .ok_or_else(|| format!("参数必须是 key=value 格式: {value}"))?;
            if key.is_empty() {
                return Err(format!("参数 key 不能为空: ={value}"));
            }
            Ok((key.to_owned(), value.to_owned()))
        })
        .collect::<Result<HashMap<_, _>, _>>()
        .map(Some)
}

fn parse_json(text: Option<String>) -> Result<Option<Value>, String> {
    let Some(text) = text else {
        return Ok(None);
    };
    let value: Value =
        serde_json::from_str(&text).map_err(|error| format!("--json 不是合法 JSON: {error}"))?;
    if !value.is_object() && !value.is_array() {
        return Err("--json 必须是 JSON object 或 array".to_owned());
    }
    Ok(Some(value))
}

fn confirm_write(prompt: &str, yes: bool) -> Result<(), String> {
    if yes {
        return Ok(());
    }
    print!("{prompt} [y/N]: ");
    io::stdout().flush().map_err(err)?;
    let mut answer = String::new();
    io::stdin().read_line(&mut answer).map_err(err)?;
    if matches!(answer.trim().to_ascii_lowercase().as_str(), "y" | "yes") {
        Ok(())
    } else {
        Err("操作已取消".to_owned())
    }
}

fn print_json(value: &Value) -> Result<(), String> {
    println!("{}", serde_json::to_string_pretty(value).map_err(err)?);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{message_content, parse_json, parse_key_values, validate_text, MessageType};
    use crate::test_support::{must, present};
    use serde_json::json;

    #[test]
    fn text_message_requires_text_and_uses_current_content_shape() {
        assert_eq!(
            message_content(&MessageType::Text, None, None, None).unwrap_err(),
            "message-type=text 需要 --text"
        );
        assert_eq!(
            must(message_content(
                &MessageType::Text,
                Some("你好".to_owned()),
                None,
                None
            )),
            json!({"msg_type": 1, "text": {"text": "你好"}})
        );
        assert!(message_content(
            &MessageType::Text,
            Some("https://example.com".to_owned()),
            None,
            None
        )
        .is_err());
        assert!(validate_text(&"".repeat(101), "评论内容", 100, false).is_err());
    }

    #[test]
    fn generic_request_parsers_reject_invalid_values() {
        assert!(parse_key_values(vec!["invalid".to_owned()]).is_err());
        assert!(parse_json(Some("1".to_owned())).is_err());
        let values = present(must(parse_key_values(vec!["open_id=value".to_owned()])));
        assert_eq!(values["open_id"], "value");
    }
}