代码之家  ›  专栏  ›  技术社区  ›  Phil Sturgeon

OmniAuth+推特、FB地点等

  •  6
  • Phil Sturgeon  · 技术社区  · 14 年前

    我正在使用OmniAuth+Devise允许用户使用附加到普通用户帐户的Facebook/Twitter/Gowalla/etc进行注册。现在,当用户使用其中任何一个或他们的帐户登录时,他们的所有社交网络都会附加在身份验证表中。

    我需要能够从这些提供商中提取内容,比如他们的tweets或他们的Facebook Places checkings等。我知道我需要使用不同的gem、plugin,无论做什么,但是获取需要使用这些gem的配置(并发出请求)对我来说都是一个困惑。

    我需要能够访问omniauth.rb中的provider配置项,这样我就有了API密钥和密钥等,然后我需要能够从oAuth中获取令牌来发出请求。

    其他宝石如 https://github.com/jrallison/authlogic_oauth 似乎存储了oauth_令牌、oauth_secret和oauth_令牌,但OmniAuth没有。

    正如您可能知道的,我对Ruby、Rails和oAuth非常陌生,所以这是一个非常有挑战性的应用程序。非常需要帮助。

    2 回复  |  直到 14 年前
        1
  •  20
  •   Phil Sturgeon    14 年前

    整理好了!

    向中描述的身份验证表添加了令牌和机密 http://railscasts.com/episodes/236-omniauth-part-2 但是更改了authentication.build行以获取另外两个参数:

    authentications.build(
        :provider => omniauth['provider'], 
        :uid => omniauth['uid'], 
        :token => omniauth['credentials']['token'], 
        :secret => omniauth['credentials']['secret']
    )
    

    然后使用 http://dev.twitter.com/pages/oauth_single_token#ruby

    class CronController < ApplicationController
    
        def recent_tweets
            # Exchange your oauth_token and oauth_token_secret for an AccessToken instance.
    
            def prepare_access_token(oauth_token, oauth_token_secret)
                consumer = OAuth::Consumer.new("APIKey", "APISecret"
                    { :site => "http://api.twitter.com"
                    })
                # now create the access token object from passed values
                token_hash = { :oauth_token => oauth_token,
                                             :oauth_token_secret => oauth_token_secret
                                         }
                access_token = OAuth::AccessToken.from_hash(consumer, token_hash )
                return access_token
            end
    
            auth = current_user.authentications.find(:first, :conditions => { :provider => 'twitter' })
    
            # Exchange our oauth_token and oauth_token secret for the AccessToken instance.
            access_token = prepare_access_token(auth['token'], auth['secret'])
    
            # use the access token as an agent to get the home timeline
            response = access_token.request(:get, "http://api.twitter.com/1/statuses/home_timeline.json")
    
            render :json => response.body
        end
    end
    

    通过从当前用户身份验证中提取内容(im查找第一个,因为它们应该只有一个),我可以获取令牌和安全性及其所有优点。

    现在我可以调整这个,保存一些东西,修改JSON,得到我需要的东西。我相信Facebook也会很相似。

        2
  •  6
  •   Jeffrey Wilcke    14 年前

    这就是你要找的吗? http://railscasts.com/episodes/236-omniauth-part-2 :) 它展示了如何检索电子邮件等数据。不知道你是不是在找这个。