导入
创建一个 Reddit 子版块并发布帖子
学习如何使用 Reddit API 创建一个子版块并发布帖子。
| 项目 | 类型前缀 |
|---|---|
| 评论 | t1_ |
| 账户 | t2_ |
| 链接 | t3_ |
| 消息 | t4_ |
| 子版块 | t5_ |
| 奖励 | t6_ |
子版块是围绕特定主题建立的社区。只要遵守该子版块的规则并获得版主允许,任何 Reddit 用户都可以向任意子版块发布内容。
任何 Reddit 用户都可以创建一个子版块。一旦创建,版主永远无法删除该子版块。子版块将始终显示最初创建它的用户。只有当创建者的账户被删除时,我们才能从该子版块中移除其创建者的信息。不过,与其删除子版块,我们也可以将其设为私有。

...
用于创建子版块的端点
1https://oauth.reddit.com/api/site_admin
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| api_type | string | 可选 | 传入 "json" 字符串以获取 JSON 格式的响应。 |
| name | string | 必需 | 指定子版块的名称。 |
| public_description | string | 必需 | 子版块的描述,应使用 Markdown 格式。 |
| title | string | 必需 | 子版块的标题。 |
| type | string | 必需 | 设置为 "public" 表示创建公开子版块。 |
1import fetch from "node-fetch"; 2 3const Name = "{{SUBREDDIT_NAME}}"; 4 5const endpointUrl = new URL("https://oauth.reddit.com/api/site_admin"); 6 7const bodyParameters = new URLSearchParams({ 8 api_type: "json", 9 name: Name, 10 title: "一个示例子版块", 11 public_description: "示例子版块的描述", 12 type: "public", 13 wikimode: "anyone", 14 link_type: "self", 15}); 16 17const headerParameters = { 18 UserAgent: "testscript by u/{{USERNAME}}", 19 authorization: "Bearer {{ACCESS_TOKEN}}", 20 accept: "application/json", 21}; 22 23// 设置 API 调用选项 24const options = { 25 method: "POST", 26 headers: headerParameters, 27 body: bodyParameters, 28}; 29 30// 执行 API 调用的函数 31async function createSubreddit() { 32 try { 33 const response = await fetch(endpointUrl, options); 34 35 // 自定义函数:打印 API 响应 36 printResponse(response); 37 } catch (error) { 38 // 自定义函数:打印错误信息 39 printError(error); 40 } 41} 42 43// 调用函数执行 API 请求 44createSubreddit();
### 响应字段
...
帖子提交端点
1https://oauth.reddit.com/api/submit
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| sr | string | 必需 | 要发布到的子版块名称。 |
| title | string | 必需 | 帖子标题。 |
| spoiler | boolean | 可选 | 用于标记该帖子为剧透内容。 |
| nsfw | boolean | 可选 | 用于标记该帖子为成人内容(NSFW)。 |
| kind | string | 必需 | 必须为以下之一:link、self、image、video、videogif。 |
| text | string | 必需 | 帖子正文,使用 Markdown 格式。 |
1// 在此处定义导入库 2import fetch from "node-fetch"; 3 4const endpointUrl = new URL("https://oauth.reddit.com/api/submit"); 5 6const bodyParameters = new URLSearchParams({ 7 api_type: "json", 8 sr: "{{SUBREDDIT_NAME}}", 9 title: "一个示例子版块帖子", 10 kind: "self", 11 text: "示例帖子的描述", 12 link_type: "self", 13}); 14 15const headerParameters = { 16 UserAgent: "testscript by u/{{USERNAME}}", 17 authorization: `Bearer {{ACCESS_TOKEN}}`, 18 contentType: "application/json", 19}; 20 21const options = { 22 method: "POST", 23 headers: headerParameters, 24 body: bodyParameters, 25}; 26 27// 执行 API 调用的函数 28async function postSubmission() { 29 try { 30 const response = await fetch(endpointUrl, options); 31 printResponse(response); 32 } catch (error) { 33 // 自定义函数:打印错误信息 34 printError(error); 35 } 36} 37 38// 调用函数执行 API 请求 39postSubmission();
### 响应字段
...
用于帖子审核的端点
1https://oauth.reddit.com/api/approve
请求参数
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| id | string | 必需 | 要批准的内容的完整名称(fullname),例如帖子为 t3_{{POST_ID}}。 |
1import fetch from "node-fetch"; 2 3const endpointUrl = new URL("https://oauth.reddit.com/api/approve"); 4 5const bodyParameters = new URLSearchParams({ 6 id: "t3_{{POST_ID}}", 7}); 8 9const headerParameters = { 10 UserAgent: "testscript by u/{{USERNAME}}", 11 authorization: `Bearer {{ACCESS_TOKEN}}`, 12 contentType: "application/json", 13}; 14 15const options = { 16 method: "POST", 17 headers: headerParameters, 18 body: bodyParameters, 19}; 20 21// 执行 API 调用的函数 22async function postApproval() { 23 try { 24 const response = await fetch(endpointUrl, options); 25 printResponse(response); 26 } catch (error) { 27 // 自定义函数:打印错误信息 28 printError(error); 29 } 30} 31 32// 调用函数执行 API 请求 33postApproval();
### 响应字段
...
已批准的帖子,现在可在我们的子版块中查看。