1

OK, I am pulling my hair after trying this out too many times to debug. So please help me out here. I keep getting 401 Unauthorized error after I am redirected back.

Here is my code. What am I doing wrong here?

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
Smi
  • 13,850
  • 9
  • 56
  • 64
Manas
  • 43
  • 4
  • Maybe you should remove the linebreak in the `@info = @access_token.get("ht....` line? – Adrian Jul 03 '10 at 20:18
  • Thnx Adrian. My code did not have a line break, its just here on stackoverflow it seems like it has one. – Manas Jul 05 '10 at 15:05

2 Answers2

1

I know absolutely nothing about Oauth and this may be completely wrong, but, if http://foursquare.com is not your local machine and oauth_callback is a URL that http://foursquare.com will call when it has completed then it will be calling back to itself as its definition of localhost will be its own 127.0.0.1 i.e itself.

If I have guessed correctly here then change the :oauth_callback to your public IP Address/name.

Steve Weet
  • 28,126
  • 11
  • 70
  • 86
0

I think @request_token = OAuth::RequestToken.new(@consumer, session[:request_token], session[:request_secret]) is wrong.

If you already have the token and the secret, you don't really need to do the verifier thing.

You should construct it like this:

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

Or if you already have the token and the secret, you should do:

access_token = OAuth::AccessToken.from_hash(consumer, {
  :oauth_token => "YOUR_TOKEN",
  :oauth_token_secret => "YOUR_SECRET"
})
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Tanin
  • 1,853
  • 1
  • 15
  • 20