2013-11-02 18 views
5

Tôi tiếp tục nhận được {"error" : 401} khi cố gắng OAuth chuyển vào Reddit bằng ruby ​​sử dụng đá quý oauth2. Các trang wiki nói rằng điều này là do thông tin không chính xác hoặc không tồn tại nhưng tôi tích cực Tôi có những cái đúng:Nhận mã lỗi 401 khi sử dụng OAuth với Reddit

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 

Trả lời

0

Đã lâu rồi và mã đã biến mất một thời gian nên không có cách nào để chẩn đoán ngay bây giờ. Đoán của tôi là làm với việc triển khai khá lỏng lẻo của khung công tác OAuth2.

Tuy nhiên, nếu bạn đang sử dụng Ruby và muốn kết nối với reddit qua OAuth2, có thể bạn thích xem redd.

Các vấn đề liên quan