首页 > 解决方案 > Reddit POST 请求 - 错误 403 (php/curl)

问题描述

我正在尝试连接 Reddit API 并以编程方式提交帖子。我已设法返回访问令牌,但在尝试进行实际提交时出现 403 错误。有什么建议吗?

获取访问令牌:(有效,返回令牌)

$ch = curl_init('https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&username=USERNAME&password=PASSWORD');
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$token = json_decode($return, true);

提交链接:(错误 403)

$ch = curl_init('https://www.reddit.com/api/v1/submit?kind=link&sr=news&title=TITLE&r=news&url='.urlencode('http://example.com'));
curl_setopt($ch, CURLOPT_USERPWD, 'CLIENTID:CLIENTSECRET');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer ".$token['access_token']));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
return json_decode($return, true);

标签: phpcurlreddit

解决方案


推荐阅读