문제

I keep getting {"error" : 401} when trying to OAuth into Reddit on ruby using the oauth2 gem. The wiki page says that this is because of incorrect or non-existent credentials but I'm positive I have the correct ones:

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
도움이 되었습니까?

해결책 2

It's been a while and the code has been gone for a while so there's no way to diagnose it now. My guess is to do with reddit's pretty loose implementation of the OAuth2 framework.

Nevertheless, if you're using Ruby and want to connect to reddit via OAuth2, maybe you might prefer taking a look at redd.

다른 팁

The site option for your Oauth2::Client should be 'https://ssl.reddit.com/api/v1/'. Check out the omniauth-reddit client options here https://github.com/jackdempsey/omniauth-reddit/blob/master/lib/omniauth/strategies/reddit.rb

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top