2013-05-13 44 views
2

我正在试试我的手在轨道上的红宝石。大部分时间我都在Sinatra编写代码。无论如何,这个问题可能不需要对框架做任何事情。这个问题听起来可能是一个非常新手的问题。我第一次玩Twitter 1.1 API和OAuth。Twitter在Ruby中三脚授权

我创建了一个应用程序XYZ并在Twitter上注册。我得到了XYZ的消费者密钥,即CONSUMER_KEY和消费者密钥,即CONSUMER_SECRET。我也得到了XYZ自己的访问令牌即ACCESS_TOKEN和访问秘密即ACCESS_SECRET

XYZ应用类型:读,写和访问直接信息 XYZ回调URL:http://www.mysite.com/cback 我已经检查:允许该应用程序可用于登录与Twitter

我所试图做的是非常简单的:

1)用户来我的网站,并点击一个链接Link your twitter account(不与Twitter签到)
2)打开叽叽喳喳弹出,其中用户同意到XYZ在他/她的行为上执行操作f)
3)一旦用户允许并且弹出窗口关闭,XYZ应用获取用户的访问令牌并保密并保存在数据库中。
4)然后XYZ使用该用户的令牌和秘密在未来执行操作。

我可能不知道这样的工作流程已经在几千个网站上实现,而Twitter API文档解释了这种三方认证,但我仍然无法弄清楚。

我已阅读https://dev.twitter.com/docs/auth/3-legged-authorizationhttps://dev.twitter.com/docs/auth/implementing-sign-twitter不幸的是没有在互联网上找到的红宝石代码,可以用一步一步的例子来解释。

当用户点击Link your twitter account时应该使用什么链接来打开twitter认证页面。 任何人都可以在这里使用我的pseduo凭证编写一些伪代码,以达到我的目标,从beging到这个工作流程结束?谢谢。

UPDATE:

我开始与请求请求令牌作为

require 'oauth'
consumer = OAuth::Consumer.new(CONSUMER_KEY, CONSUMER_SECRET,
{ site: "https://twitter.com"})
request_token = consumer.get_request_token oauth_callback: 'http://www.mysite.com/tauth'
redirect_to request_token.authorize_url

+0

你应该看看如何[omniauth-叽叽喳喳(https://github.com/ arunagw/omniauth-twitterhttps://github.com/arunagw/omniauth-twitter)实现它。它可能只是有你在找什么。 :) – kiddorails 2013-05-13 07:12:59

回答

1

我不熟悉的ROR,但这里是OAuth的“舞蹈”,你需要时要遵循的工作流程用户点击你的按钮:

  1. 通过发送一个从Twitter获得未经授权的请求令牌10请求

    POST https://api.twitter.com/oauth/request_token

    签约使用消费者的秘密请求。这将在后台完成,并且 对用户将是透明的。

  2. 您将收到来自 twitter的oauth_token和oauth_token_secret。

  3. 将用户重定向到

    https://api.twitter.com/oauth/authorize?组oauth_token = [token_received_from_twitter]

    使用您从Twitter在步骤接收OAuth令牌值2

  4. 当用户授权应用程式它们将被重定向到您的 回调URL与组oauth_token和oauth_verifier附加到 网址。即

    http://www.mysite.com/cback?oauth_token=NPcudxy0yU5T3tBzho7iCotZ3cnetKwcTIRlX0iwRl0&oauth_verifer=uw7NjWHT6OJ1MpJOXsHfNxoAhPKpgI8BlYDhxEjIBY

  5. 通过与oauth_verifier一起发送签署 请求

    POST https://api.twitter.com/oauth/access_token

    与消费者的秘密签署您的要求 转换请求令牌到一个访问令牌和在步骤2中接收的令牌密码。

  6. 如果一切顺利,您将收到来自Twitter的新oauth_tokenoauth_token_secret。这是您的 用户的访问令牌。

  7. 使用步骤6中收到的访问令牌和密码,您可以通过将签名请求 发送给相应的api端点来代表用户拨打 Twitter api调用。

+1

是否有简化或向我们展示如何在代码中执行此操作的宝石或要点? – cevaris 2014-09-11 01:51:07

+0

@cevaris,你能够实现这一点。 – 2016-05-06 07:55:44

+0

@我是简单用户Yup,https://github.com/cevaris/twitter-oauth/blob/master/main.rb。似乎'omniauth-twitter'没有办法。 – cevaris 2016-05-06 13:26:47

1

希望大家通过这次解决您的问题,但我建立了这个样本登录与Twitter红宝石的Web应用程序提供你需要做这种整合所有的解释。下面有一个实现带注释的所有必要方法的类:

require "net/https" 
require "simple_oauth" 

# This class implements the requests that should 
# be done to Twitter to be able to authenticate 
# users with Twitter credentials 
class TwitterSignIn 

class << self 
    def configure 
    @oauth = YAML.load_file(TWITTER) 
    end 

    # See https://dev.twitter.com/docs/auth/implementing-sign-twitter (Step 1) 
    def request_token 

    # The request to get request tokens should only 
    # use consumer key and consumer secret, no token 
    # is necessary 
    response = TwitterSignIn.request(
     :post, 
     "https://api.twitter.com/oauth/request_token", 
     {}, 
     @oauth 
    ) 

    obj = {} 
    vars = response.body.split("&").each do |v| 
     obj[v.split("=").first] = v.split("=").last 
    end 

    # oauth_token and oauth_token_secret should 
    # be stored in a database and will be used 
    # to retrieve user access tokens in next requests 
    db = Daybreak::DB.new DATABASE 
    db.lock { db[obj["oauth_token"]] = obj } 
    db.close 

    return obj["oauth_token"] 
    end 

    # See https://dev.twitter.com/docs/auth/implementing-sign-twitter (Step 2) 
    def authenticate_url(query) 
    # The redirection need to be done with oauth_token 
    # obtained in request_token request 
    "https://api.twitter.com/oauth/authenticate?oauth_token=" + query 
    end 

    # See https://dev.twitter.com/docs/auth/implementing-sign-twitter (Step 3) 
    def access_token(oauth_token, oauth_verifier) 

    # To request access token, you need to retrieve 
    # oauth_token and oauth_token_secret stored in 
    # database 
    db = Daybreak::DB.new DATABASE 
    if dbtoken = db[oauth_token] 

     # now the oauth signature variables should be 
     # your app consumer keys and secrets and also 
     # token key and token secret obtained in request_token 
     oauth = @oauth.dup 
     oauth[:token] = oauth_token 
     oauth[:token_secret] = dbtoken["oauth_token_secret"] 

     # oauth_verifier got in callback must 
     # to be passed as body param 
     response = TwitterSignIn.request(
     :post, 
     "https://api.twitter.com/oauth/access_token", 
     {:oauth_verifier => oauth_verifier}, 
     oauth 
     ) 

     obj = {} 
     vars = response.body.split("&").each do |v| 
     obj[v.split("=").first] = v.split("=").last 
     end 

     # now the we got the access tokens, store it safely 
     # in database, you're going to use it later to 
     # access Twitter API in behalf of logged user 
     dbtoken["access_token"] = obj["oauth_token"] 
     dbtoken["access_token_secret"] = obj["oauth_token_secret"] 
     db.lock { db[oauth_token] = dbtoken } 

    else 
     oauth_token = nil 
    end 

    db.close 
    return oauth_token 
    end 

    # This is a sample Twitter API request to 
    # make usage of user Access Token 
    # See https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials 
    def verify_credentials(oauth_token) 
    db = Daybreak::DB.new DATABASE 

    if dbtoken = db[oauth_token] 

     # see that now we use the app consumer variables 
     # plus user access token variables to sign the request 
     oauth = @oauth.dup 
     oauth[:token] = dbtoken["access_token"] 
     oauth[:token_secret] = dbtoken["access_token_secret"] 

     response = TwitterSignIn.request(
     :get, 
     "https://api.twitter.com/1.1/account/verify_credentials.json", 
     {}, 
     oauth 
     ) 

     user = JSON.parse(response.body) 

     # Just saving user info to database 
     user.merge! dbtoken 
     db.lock { db[user["screen_name"]] = user } 

     result = user 

    else 
     result = nil 
    end 

    db.close 
    return result 
    end 

    # Generic request method used by methods above 
    def request(method, uri, params, oauth) 
    uri = URI.parse(uri.to_s) 

    # always use SSL, you are dealing with other users data 
    http = Net::HTTP.new(uri.host, uri.port) 
    http.use_ssl = true 
    # uncomment line below for debug purposes 
    #http.set_debug_output($stdout) 

    req = (method == :post ? Net::HTTP::Post : Net::HTTP::Get).new(uri.request_uri) 
    req.body = params.to_a.map { |x| "#{x[0]}=#{x[1]}" }.join("&") 
    req["Host"] = "api.twitter.com" 

    # Oauth magic is done by simple_oauth gem. 
    # This gem is enable you to use any HTTP lib 
    # you want to connect in OAuth enabled APIs. 
    # It only creates the Authorization header value for you 
    # and you can assign it wherever you want 
    # See https://github.com/laserlemon/simple_oauth 
    req["Authorization"] = SimpleOAuth::Header.new(method, uri.to_s, params, oauth) 

    http.request(req) 
    end 

    end 
end 

更详细的解释为: https://github.com/lfcipriani/sign_in_with_twitter_sample

+0

嗨,欢迎来到SO。请注意,答案应该包含细节而不是链接,因为链接可能会改变。请考虑编辑您的答案并添加相关代码。 – Noich 2013-11-28 13:11:23

+1

完成!该代码代表了核心解决方案 – Cipriani 2013-12-16 14:41:54