【问题标题】:Ruby - reddit oauth returns 404Ruby - reddit oauth 返回 404
【发布时间】:2016-11-25 13:40:54
【问题描述】:

我正在尝试使用 HTTParty 从 reddit 获取身份验证令牌。我正在使用 reddit 的脚本身份验证,这在 here 中进行了解释。

该页面的 curl 版本如下所示。如果我在其中输入我的详细信息,那么它可以正常工作并返回身份验证令牌。

curl -X POST -d 'grant_type=password&username=reddit_bot&password=snoo' --user 'p-jcoLKBynTLew:gko_LXELoV07ZBNUXrvWZfzE3aI' https://www.reddit.com/api/v1/access_token

这是我的 ruby​​ 代码,其中的身份验证实现不起作用。

require 'httparty'
require 'logger'

class Reddit
  include HTTParty
  base_uri 'https://reddit.com'
  logger ::Logger.new("httparty.log"), :debug, :curl

  def initialize(client_id, client_secret, username, password, user_agent)
    @auth = { username: client_id, password: client_secret }
    @username = username
    @password = password
    @user_agent = user_agent
  end

  def authenticate()
    options = { 
      basic_auth: @auth,
      body: { grant_type: 'password', username: @username, password: @password }, 
      headers: { 'User-Agent' => @user_agent }
    }
    self.class.post('/api/v1/access_token', options)
  end
end

这是我的日志

D, [2016-07-21T20:21:29.581864 #10739] DEBUG -- : [HTTParty] [2016-07-21 20:21:29 +0100] > POST /api/v1/access_token
[HTTParty] [2016-07-21 20:21:29 +0100] > User-Agent: MY_USER_AGENT
[HTTParty] [2016-07-21 20:21:29 +0100] > grant_type=password&username=MYUSERNAME&password=MYPASSWORD
[HTTParty] [2016-07-21 20:21:29 +0100] > 
[HTTParty] [2016-07-21 20:21:29 +0100] < HTTP/1.1 301
[HTTParty] [2016-07-21 20:21:29 +0100] < Date: Thu, 21 Jul 2016 19:21:29 GMT
[HTTParty] [2016-07-21 20:21:29 +0100] < Transfer-encoding: chunked
[HTTParty] [2016-07-21 20:21:29 +0100] < Connection: close
[HTTParty] [2016-07-21 20:21:29 +0100] < Set-cookie: __cfduid=d1653e1497aa10c1e9b5c9c1e65027f841469128889; expires=Fri, 21-Jul-17 19:21:29 GMT; path=/; domain=.reddit.com; HttpOnly
[HTTParty] [2016-07-21 20:21:29 +0100] < Location: https://www.reddit.com/api/v1/access_token
[HTTParty] [2016-07-21 20:21:29 +0100] < Strict-transport-security: max-age=15552000; includeSubDomains; preload
[HTTParty] [2016-07-21 20:21:29 +0100] < X-content-type-options: nosniff
[HTTParty] [2016-07-21 20:21:29 +0100] < Server: cloudflare-nginx
[HTTParty] [2016-07-21 20:21:29 +0100] < Cf-ray: 2c6104281c703500-LHR
[HTTParty] [2016-07-21 20:21:29 +0100] < 

D, [2016-07-21T20:21:30.930118 #10739] DEBUG -- : [HTTParty] [2016-07-21 20:21:30 +0100] > GET https://www.reddit.com/api/v1/access_token
[HTTParty] [2016-07-21 20:21:30 +0100] > User-Agent: MY_USER_AGENT
[HTTParty] [2016-07-21 20:21:30 +0100] > Cookie: __cfduid=d1653e1497aa10c1e9b5c9c1e65027f841469128889
[HTTParty] [2016-07-21 20:21:30 +0100] > grant_type=password&username=MYUSERNAME&password=MYPASSWORD
[HTTParty] [2016-07-21 20:21:30 +0100] > 
[HTTParty] [2016-07-21 20:21:30 +0100] < HTTP/1.1 404
[HTTParty] [2016-07-21 20:21:30 +0100] < Date: Thu, 21 Jul 2016 19:21:30 GMT
[HTTParty] [2016-07-21 20:21:30 +0100] < Content-type: application/json; charset=UTF-8
[HTTParty] [2016-07-21 20:21:30 +0100] < Content-length: 38
[HTTParty] [2016-07-21 20:21:30 +0100] < Connection: close
[HTTParty] [2016-07-21 20:21:30 +0100] < X-frame-options: SAMEORIGIN
[HTTParty] [2016-07-21 20:21:30 +0100] < X-content-type-options: nosniff
[HTTParty] [2016-07-21 20:21:30 +0100] < X-xss-protection: 1; mode=block
[HTTParty] [2016-07-21 20:21:30 +0100] < X-ua-compatible: IE=edge
[HTTParty] [2016-07-21 20:21:30 +0100] < Access-control-allow-origin: *
[HTTParty] [2016-07-21 20:21:30 +0100] < Access-control-expose-headers: X-Reddit-Tracking, X-Moose
[HTTParty] [2016-07-21 20:21:30 +0100] < X-reddit-tracking: https://pixel.redditmedia.com/pixel/of_destiny.png?v=cPS4SYC7eYS7PUX%2B4OpG5Vd8whFwi9HO%2Btj9EkiJOG%2BX0pPXV71coHXAWrCRqDNvm8svDX3GBsMwOXsOxbGTRaDC%2BRJqtSoM
[HTTParty] [2016-07-21 20:21:30 +0100] < Cache-control: max-age=0, must-revalidate
[HTTParty] [2016-07-21 20:21:30 +0100] < X-moose: majestic
[HTTParty] [2016-07-21 20:21:30 +0100] < Strict-transport-security: max-age=15552000; includeSubDomains; preload
[HTTParty] [2016-07-21 20:21:30 +0100] < Server: cloudflare-nginx
[HTTParty] [2016-07-21 20:21:30 +0100] < Cf-ray: 2c61042b711c3470-LHR
[HTTParty] [2016-07-21 20:21:30 +0100] < 
{"message": "Not Found", "error": 404}

【问题讨论】:

    标签: ruby oauth httparty reddit


    【解决方案1】:

    您似乎在初始请求中遇到了从 https://reddit.comhttps://www.reddit.com 的 301 重定向。

    当 HTTParty 遵循重定向时,它会执行 GET 请求而不是 POST(应该如此)。

    尝试将您的 base_uri 改为“https://www.reddit.com”。

    【讨论】:

    • 啊,我明白了,现在说得通了。非常感谢!
    猜你喜欢
    • 2017-09-16
    • 2013-01-06
    • 2015-05-28
    • 2014-06-01
    • 1970-01-01
    • 2015-08-14
    • 2017-06-19
    • 2020-08-11
    • 2012-09-26
    相关资源
    最近更新 更多