views:

299

answers:

1

I am trying to get the access key but I cannot make it work. `request_token.get_access_token is giving me 401 Unauthorized (OAuth::Unauthorized) error. I copy the authorize_url into my browser, allow the application, I receive some kind of PIN from twitter but after hitting enter in my script I always get 401 error. I did some search and I found this helped access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier]) but it is giving me undefined local variable or methodparams' for main:Object (NameError)`

  • the twitter application type is client
  • the ruby script is like ( I was following this tutorial )
  • I'd love to get out of this scrip access details. The best without using PIN.

.

gem 'oauth'
require 'oauth/consumer'

consumer_key = 'your key'
consumer_secret ='your secret'

consumer=OAuth::Consumer.new "consumer_key", 
                          "consumer_secret", 
                          {:site=>"http://twitter.com"}

request_token = consumer.get_request_token

puts request_token.token
puts request_token.secret
puts request_token.authorize_url
puts "Hit enter when you have completed authorization."
STDIN.gets

access_token = request_token.get_access_token
#access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])

puts access_token.token
puts access_token.secret
puts
puts access_token.inspect
+2  A: 

I was having the exact same problem. replace:

STDIN.gets

with:

pin = (gets.chomp).to_i

The to_i converts to integer which also acts to strip whitespace. Then you need to specify the pin when trying to get the acess token. If you do not do this there is no proof that the user has allowed your application.

access_token = request_token.get_access_token(:oauth_verifier => pin)

puts "Access Token  : " + access_token.token
puts "Access Secret : " + access_token.secret
Munkymorgy
when used your code I received `undefined local variable or method `oauth' for main:Object (NameError)`
Radek
That was my mistake in the first example posted. I have re written my answer which makes the previuos comment irrelevant. The current answer should fix the original problem
Munkymorgy
@Munkymorgy: great! works nicely. I can do that procedure of obtaining secret access details once and the use it later on.
Radek