我在使用{"error" : 401}
gem在ruby上尝试使用OAuth进入Reddit时不断获得oauth2
。维基页面说这是因为凭据不正确或不存在,但我很肯定我有正确的凭证:
require "oauth2"
require "base64"
reddit = OAuth2::Client.new ENV["API_ID"], ENV["API_SECRET"], \
:authorize_url => "https://ssl.reddit.com/api/v1/authorize",
:token_url => "https://ssl.reddit.com/api/v1/access_token",
:site => "https://oauth.reddit.com/api/v1/"
state = Digest::SHA1.hexdigest rand(36**8).to_s(36)
redirect_uri = "http://localhost:8080/oauth2/callback"
params = {"scope" => "identity",
"response_type" => "code",
"redirect_uri" => redirect_uri,
"state" => state,
"duration" => "permanent"
}
puts reddit.auth_code.authorize_url params
# Get the url with the code that reddit redirects to
redir = gets.chomp.strip
code = redir.match(/code=([^&]*)/).captures
returned_state = redir.match(/state=([^&]*)/).captures
raise "State does not Match!" unless state === returned_state[0]
params = {"scope" => "identity",
"redirect_uri" => redirect_uri,
"state" => state
}
token = reddit.auth_code.get_token(code[0], params, :headers => {'Authorization' => "Basic " + Base64.strict_encode64('#{ENV["API_ID"]}:#{ENV["API_SECRET"]}')})
response = token.get('me')
puts response
答案 0 :(得分:2)
Oauth2 :: Client的网站选项应为“https://ssl.reddit.com/api/v1/”。在这里查看omniauth-reddit客户端选项https://github.com/jackdempsey/omniauth-reddit/blob/master/lib/omniauth/strategies/reddit.rb
答案 1 :(得分:0)
已经有一段时间了,代码已经消失了一段时间,所以现在无法对其进行诊断。我的猜测是使用reddit非常松散的OAuth2框架实现。
然而,如果您正在使用Ruby并希望通过OAuth2连接到reddit,那么您可能更愿意看一下redd。