2010-07-03 211 views
1

好吧,我試了很多次之後就拉着我的頭髮去調試。 所以請在這裏幫助我。在我重新導向後,我一直收到401 Unauthorized error401未經授權的foursquare OAuth

這是我的代碼。我在這裏做錯了什麼?

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 
+0

也許你應該刪除斷行的'@info = @ access_token.get(「ht ....'line? – Adrian 2010-07-03 20:18:27

+0

Thnx Adrian。我的代碼沒有換行符,它只是在stackoverflow上,它似乎有一個。 – Manas 2010-07-05 15:05:59

+0

你解決了這個問題嗎? – ck3g 2012-02-07 08:55:59

回答

1

我知道絕對沒有什麼瞭解OAuth,這可能是完全錯誤的,但是,如果http://foursquare.com是不是你的本地計算機和oauth_callback是一個URL,當它完成,然後將回調到自身http://foursquare.com將調用因爲它的本地主機的定義將是它自己的127.0.0.1即它本身。

如果我在這裏猜到了,然後將:oauth_callback更改爲您的公共IP地址/名稱。

0

我認爲@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" 
}) 
相關問題