简体   繁体   English

无法使用Rails和OAuth gem获得我的access_token

[英]Can't get my access_token using Rails and the OAuth gem

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. 我发展我的Rails 3应用程序,使用Twitter的OAuth的和我得到的麻烦,因为显然我不能拿到的access_token,点击“允许”和Twitter重定向我回到我的应用程序URL,当我去twitter.com/settings后/ connections我看不到我的应用程序被授权。 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. 罗德里戈·阿尔维斯·维埃拉(Rodrigo Alves Vieira)。

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.. 我不确定代码的哪一部分不起作用,可能与access_token_get方法有关,但是我将向您展示如何做到这一点-也许会有所帮助。

after the line where you initialize @access_token, I do something like this: 在初始化@access_token的行之后,我执行以下操作:

@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) (哦,我正在使用json gem,因此请确保gem install json并在顶部require 'json'

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM