【问题标题】:(REDDIT) Error trying to subscribe to subreddits via API(REDDIT) 尝试通过 API 订阅 subreddits 时出错
【发布时间】:2014-12-02 07:54:30
【问题描述】:

我知道Snoo 似乎无人维护,但我想使用 ruby​​ 框架,因为我正在努力提高我的 Ruby 技能。

我正在尝试添加一些从订阅和取消订阅 subreddits 开始的功能。 Link to API doc.

我的第一次尝试是使用返回 404 错误的内置 post 方法

def subscribe(subreddit)
    logged_in?
    post('/api/subscribe.json',body:{uh: @modhash, action:'sub', sr: subreddit, api_type: 'json'})
end

由于内置的​​ post 方法给了我 404,我决定尝试 HTTParty post 方法:

def subscribe(subreddit)
    logged_in?
    HTTParty.post('http://www.reddit.com/api/subscribe.json',body:{uh: @modhash, action:'sub', sr: subreddit, api_type: 'json'})
end

返回这个:

pry(main)> reddit.subscribe('/r/nba')
=> {"json"=>{"errors"=>[["USER_REQUIRED", "please login to do that", nil]]}}

有谁知道我是否需要在正文中传递更多信息,或者我是否只是发送格式错误的请求?谢谢!

另外,在运行“reddit.subscribe”之前,我已经验证我使用 cookie、modhash 登录,可以访问我的帐户信息等。

【问题讨论】:

    标签: ruby reddit


    【解决方案1】:

    找到解决方案:

    def subscribe(subreddit)
      #query the subreddit for it's 'about' info and get json back
      subreddit_json = self.subreddit_info(subreddit)
    
      #build the coded unique identifier for the targeted subreddit
      subreddit_id = subreddit_json['kind'] + "_" + subreddit_json['data']['id']
    
      #send post request to server
      server_response = self.class.post('/api/subscribe.json',
        body:{uh:@modhash, action:'sub', sr: subreddit_id, api_type:'json'})
    end
    

    Reddit API 不接受 subreddit 名称作为与 'sr' 一起传递的值(例如 sr:'/r/funny')。它需要 subreddit 的“类型”(对于 subreddit 始终是 't5')和唯一的论坛 ID。传递的参数类似于:sr: "t5_2qo4s"。如果您转到目标 subreddit 并添加 about.json,则此信息可用,例如 www.reddit.com/r/funny/about.json

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-07
      • 2014-12-11
      • 2018-08-09
      • 2018-06-25
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 2017-01-04
      相关资源
      最近更新 更多