【问题标题】:401 Unauthorized for foursquare OAuth401 Unauthorized forfoursquare OAuth
【发布时间】:2011-03-11 11:59:04
【问题描述】:

好的,在尝试了太多次以进行调试后,我正在拉扯头发。 所以请在这里帮助我。重定向回来后,我不断收到401 Unauthorized error

这是我的代码。我在这里做错了什么?

require 'rubygems'
require 'OAuth'
require 'json'

class SessionController < ApplicationController

   before_filter :load_oauth

   def index

     if session[:request_token] && params[:oauth_token]
         @request_token = OAuth::RequestToken.new(@consumer,
session[:request_token], session[:request_secret])
         @access_token =
@request_token.get_access_token(:oauth_verifier =>
params[:oauth_verifier])
         puts @access_token
         @info = @access_token.get("http://api.foursquare.com/v1/
test")

         flash[:notice] = "Foursquare! Yay!"

     else
         redirect_to(@foursqrurl)
     end

   end

   private
   def load_oauth
     @oauth_key = 'key'
     @oauth_secret = 'secret'
     @consumer = OAuth::Consumer.new(@oauth_key,@oauth_secret,{
      :site               => "http://foursquare.com",
      :scheme             => :header,
      :http_method        => :post,
      :request_token_path => "/oauth/request_token",
      :access_token_path  => "/oauth/access_token",
      :authorize_path     => "/oauth/authorize"
     })

     @request_token = @consumer.get_request_token(:oauth_callback =>
"http://localhost:3001/session")
     session[:request_token] = @request_token.token
     session[:request_secret] = @request_token.secret
     puts @request_token.token
     puts @request_token.secret
     # redirecting user to foursquare to authorize
     @foursqrurl = @request_token.authorize_url
     puts @foursqrurl

   end

end

【问题讨论】:

  • 也许您应该删除@info = @access_token.get("ht.... 行中的换行符?
  • 谢谢阿德里安。我的代码没有换行符,它就在 stackoverflow 上,好像有一个。

标签: ruby-on-rails ruby oauth foursquare oauth-ruby


【解决方案1】:

我对 Oauth 完全一无所知,这可能是完全错误的,但是,如果 http://foursquare.com 不是您的本地计算机并且 oauth_callback 是一个 URL,http://foursquare.com 在完成时将调用它,那么它将回调自身因为它对 localhost 的定义将是它自己的 127.0.0.1 即它自己。

如果我在这里猜对了,那么将 :oauth_callback 更改为您的公共 IP 地址/名称。

【讨论】:

    【解决方案2】:

    我认为@request_token = OAuth::RequestToken.new(@consumer, session[:request_token], session[:request_secret]) 是错误的。

    如果你已经有了令牌和秘密,你就不需要做验证者的事情了。

    你应该这样构造它:

    OAuth::RequestToken.from_hash(consumer, { :oauth_token => params[:oauth_token] })
    access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])
    

    或者如果你已经拥有令牌和秘密,你应该这样做:

    access_token = OAuth::AccessToken.from_hash(consumer, {
      :oauth_token => "YOUR_TOKEN",
      :oauth_token_secret => "YOUR_SECRET"
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2014-08-19
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多