导入
创建一个 Reddit 子版块并发布帖子
学习如何使用 Reddit API 创建一个子版块并发布帖子。
| 物品 | 类型前缀 |
|---|---|
| 评论 | t1_ |
| 账户 | t2_ |
| 链接 | t3_ |
| 消息 | t4_ |
| 子版块 | t5_ |
| 奖励 | t6_ |
1https://oauth.reddit.com/api/site_admin
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| api_type | string | 可选 | 传入字符串 "json" 以接收 JSON 格式的响应。 |
| name | string | 必需 | 指定子版块的名称。 |
| public_description | string | 必需 | 子版块的描述,应使用 Markdown 格式。 |
| title | string | 必需 | 子版块的标题。 |
| type | string | 必需 | 设置为 "public" 可使子版块公开。 |
1{ 2 "json": { 3 "errors": [] 4 } 5}
1import requests 2import json 3 4name = "{{SUBREDDIT_NAME}}" 5 6url = "https://oauth.reddit.com/api/site_admin" 7 8params = { 9 "api_type": "json", 10 "name": name, 11 "public_description": "一个子版块", 12 "title": "这个子版块的标题", 13 "type": "public", 14 "wikimode": "anyone", 15 "link_type": "self" 16} 17 18headers = { 19 'User-Agent': 'testscript by u/{{USERNAME}}', 20 'Authorization': "bearer {{ACCESS_TOKEN}}", 21 "Accept": "application/x-www-form-urlencoded" 22} 23 24response = requests.post(url, headers=headers, params=params) 25 26if response.__dict__["reason"] == "Unauthorized": 27 print("请前往“刷新访问令牌”课程重新获取令牌。") 28elif response.__dict__["status_code"] != 200: 29 print(f"{response.text}") 30else: 31 print(json.dumps(response.json(), indent=4))
1https://oauth.reddit.com/api/submit
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| sr | string | 必需 | 目标子版块的名称(不带 r/ 前缀),例如 redditdev。 |
| title | string | 必需 | 帖子的标题。 |
| spoiler | boolean | 可选 | 用于标记该帖子为剧透内容。 |
| nsfw | boolean | 可选 | 用于标记该帖子为成人内容(NSFW)。 |
| kind | string | 必需 | 必须是以下之一:link、self、image、video、videogif。 |
| text | string | 必需 | 帖子正文,使用 Markdown 格式。 |
1import requests 2import json 3 4url = "https://oauth.reddit.com/api/submit" 5 6params = { 7 'sr': '{{SUBREDDIT_NAME}}', 8 'title': '帖子的标题!', 9 'kind': 'self', 10 'api_type': 'json', 11 'text': '帖子正文示例。', 12 # 'validate_on_submit': 'true', 13 # 'submit_type': 'subreddit', 14 # 'show_error_list': 'true', 15 # 'spoiler': 'true', 16 # 'nsfw': 'false', 17 # 'original_content': 'false', 18} 19 20headers = { 21 'User-Agent': 'testscript by u/{{USERNAME}}', 22 'Authorization': "bearer {{ACCESS_TOKEN}}", 23 "Accept": "application/x-www-form-urlencoded" 24} 25 26response = requests.post(url, headers=headers, params=params) 27 28if response.__dict__["reason"] == "Unauthorized": 29 print("请前往“刷新访问令牌”课程重新获取令牌。") 30elif response.__dict__["status_code"] != 200: 31 print(f"{response.text}") 32elif response.__dict__["status_code"] == 200: 33 print(json.dumps(response.json(), indent=4))
1https://oauth.reddit.com/api/approve
| 参数 | 类型 | 分类 | 描述 |
|---|---|---|---|
| id | string | 必需 | 要批准的内容的完整名称(fullname),例如帖子的 t3_xxx。 |
1import requests 2import json 3 4url = "https://oauth.reddit.com/api/approve" 5 6params = { 7 'id': 't3_{{POST_ID}}', 8} 9 10headers = { 11 'User-Agent': 'testscript by u/{{USERNAME}}', 12 'Authorization': "bearer {{ACCESS_TOKEN}}", 13 "Accept": "application/x-www-form-urlencoded" 14} 15 16response = requests.post(url, headers=headers, params=params) 17 18if response.__dict__["reason"] == "Unauthorized": 19 print("请前往“刷新访问令牌”课程重新获取令牌。") 20elif response.__dict__["status_code"] != 200: 21 print(f"{response.text}") 22else: 23 print(json.dumps(response.json(), indent=4))
...
已批准的帖子现在可在我们的子版块中查看。