views:

154

answers:

1

Hello everyone, I am developing my Rails 3 application that uses Twitter OAuth and I am getting troubles because apparently I can't get the access_token, after clicking 'Allow' and Twitter redirecting me back to my application url, when I go to twitter.com/settings/connections I can't see my app there as authorized. I guess there is something wrong in my controller, I hope you can point them:

class OauthController < ApplicationController
  def start
    request_token = client.get_request_token(:oauth_callback => 'http://localhost:3000')
    session[:request_token] = request_token.token
    session[:request_token_secret] = request_token.secret
    redirect_to request_token.authorize_url
  end

  def callback
    @access_token = client.get_access_token(:oauth_verifier => params[:oauth_verifier])
    render :json => access_token_get('https://api.twitter.com/account/verify_credentials.json')
  end

  protected 

  def client
    @consumer = OAuth::Consumer.new(
      'key','secret',
      :site => 'https://api.twitter.com',
      :authorize_url => 'https://api.twitter.com/oauth/authorize',
      :access_token_url => 'https://api.twitter.com/oauth/access_token'
    )
  end
end

Please help, tell me where is my mistake, thanks for the attention!

Rodrigo Alves Vieira.

A: 

I'm not exactly sure what part of your code isn't working, probably something to do with the access_token_get method, but I'll show you how I did it--maybe it'll help..

after the line where you initialize @access_token, I do something like this:

@response = client.request(:get, "/account/verify_credentials.json", @access_token, { :scheme => :query_string })
case @response
when Net::HTTPSuccess
  user_info = JSON.parse(@response.body)
  unless user_info['screen_name']
    # authentication failed, error handling
  end
  # We have an authorized user, save the information to the database using @access_token.token and @acess_token.secret
else
  # error handling
end

(oh, and i was using the json gem, so make sure to gem install json and require 'json' at the top)

Hope that helps!

mportiz08
This did not work, but thank you very much anyway!
rodrigo3n