2013-05-13 36 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