
本文详解如何通过 php curl 实现 reddit oauth 2.0 认证并提交链接/文本帖至任意 subreddit,涵盖应用注册、令牌获取、授权头设置及常见错误规避。
本文详解如何通过 php curl 实现 reddit oauth 2.0 认证并提交链接/文本帖至任意 subreddit,涵盖应用注册、令牌获取、授权头设置及常见错误规避。
Reddit 的 API 已全面强制启用 OAuth 2.0 认证,直接使用用户名密码(即 password grant type)已不可行——该方式自 2021 年起被 Reddit 官方弃用。当前唯一合规路径是采用 code 授权码模式(推荐)或 client_credentials 模式(仅限无用户上下文的公开读取);而原示例中使用的 grant_type=password 不仅存在严重安全风险(明文传输凭证),且在现代 Reddit 应用配置中默认禁用,会导致 401 Unauthorized 或 invalid_grant 错误。
✅ 正确流程如下:
-
注册 Reddit 应用
登录 https://www.php.cn/link/625c8a8511b72a84f7c62164815f8c17,点击 “Create App”:- App type 选择 "script"(适用于个人脚本,支持 code + refresh_token)
- 设置 Redirect URI(如 http://localhost:8000/callback.php,开发阶段可填此值)
- 记录生成的 client_id(非 clientID)与 client_secret
-
获取授权码(前端跳转)
用户需访问以下 URL 授权(需替换 CLIENT_ID 和 REDIRECT_URI):https://www.reddit.com/api/v1/authorize?client_id=CLIENT_ID&response_type=code&state=random_string&redirect_uri=REDIRECT_URI&scope=submit%20identity
⚠️ scope=submit identity 是发布必需权限;其他常用 scope 可参考 Reddit OAuth Scopes
CurlShip Directory Submission下载向CurlShip提交产品,这是一个对机器人友好的SaaS目录。只需一条curl命令即可发布产品,支持OG标签抓取、带徽章的dofollow链接及层级升级。
-
后端换取 Access Token(关键步骤)
用户授权后重定向至 REDIRECT_URI?code=ABC...,服务端用 code 换取 token:<?php $clientID = 'your_client_id_here'; $clientSecret = 'your_client_secret_here'; $redirectUri = 'http://localhost:8000/callback.php'; $code = $_GET['code'] ?? ''; // Step 1: Exchange code for access_token $tokenUrl = 'https://www.reddit.com/api/v1/access_token'; $postData = http_build_query([ 'grant_type' => 'authorization_code', 'code' => $code, 'redirect_uri' => $redirectUri ]); $ch = curl_init($tokenUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $postData, CURLOPT_USERPWD => "$clientID:$clientSecret", CURLOPT_USERAGENT => 'PHP-Reddit-Client/1.0', CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'], ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { throw new Exception("Token exchange failed: $httpCode"); } $tokenData = json_decode($response, true); $accessToken = $tokenData['access_token']; $refreshToken = $tokenData['refresh_token']; // 用于后续长期访问 -
提交帖子(使用 Bearer Token)
使用获取的 access_token 调用 /api/submit:$title = 'My First Reddit Post via PHP'; $subreddit = 'learnphp'; // 注意:不带 r/ 前缀 $url = 'https://example.com/article'; // 链接帖;若发文字帖,改用 `text` 参数 $submitUrl = 'https://oauth.reddit.com/api/submit'; $submitData = http_build_query([ 'kind' => 'link', // 或 'self'(文字帖) 'sr' => $subreddit, 'title' => $title, 'url' => $url, // 若 kind='self',则用 'text' => 'Hello world!' ]); $ch = curl_init($submitUrl); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $submitData, CURLOPT_USERAGENT => 'PHP-Reddit-Client/1.0', CURLOPT_HTTPHEADER => [ "Authorization: Bearer $accessToken", 'Content-Type: application/x-www-form-urlencoded' ], ]); $result = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) { echo "✅ Post submitted successfully!\n"; print_r(json_decode($result, true)); } else { echo "❌ Submission failed (HTTP $httpCode): $result\n"; }
? 重要注意事项:
- ❌ 禁止硬编码用户名/密码;password grant 已废弃,且违反 Reddit API Terms;
- ✅ 始终设置合法 User-Agent(格式:
: : (by /u/ ),否则返回 429 Too Many Requests 或 403 Forbidden; - ? access_token 默认有效期 1 小时,建议配合 refresh_token 自动续期;
- ? 提交前确保目标 subreddit 允许外部链接(检查 user_can_submit 字段);
- ? 开发时先用 sandbox 模式测试(如向 r/test 投稿),避免触发反垃圾机制。
掌握这一流程后,你即可构建稳定、合规的 Reddit 自动化发布系统——无论是内容聚合、监控告警还是社区运营工具,都具备了底层 API 支持能力。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










