OmniAuth +拉动推文,FB场所等

Phi*_*eon 6 ruby-on-rails ruby-on-rails-plugins omniauth ruby-on-rails-3

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

我需要能够从任何这些提供商中提取内容,例如他们的推文或他们的Facebook Places检查等.我明白我需要使用不同的gem,插件,无论做什么,但得到我需要的配置使用这些宝石(并提出请求)让我很困惑.

我需要能够访问omniauth.rb中的提供程序配置项,因此我有API密钥和密钥等,然后我需要能够从oAuth的东西中获取令牌来发出请求.

像https://github.com/jrallison/authlogic_oauth这样的其他宝石似乎存储了oauth_token,oauth_secret和oauth_token,但是OmniAuth没有.

你可能会告诉我,我对Ruby,Rails和oAuth都是新手,所以这是一个非常具有挑战性的应用程序.非常需要帮助.

Phi*_*eon 20

排序!

在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']
)
Run Code Online (Sandbox Code Playgroud)

然后使用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
Run Code Online (Sandbox Code Playgroud)

通过从current_user.authentications(我找到第一个,因为他们应该只有一个)中提取内容,我可以获取令牌和安全性,并且它很好.

现在我可以调整一下,保存一些东西,然后使用JSON,并采取我需要的东西.我相信Facebook会非常相似.


小智 6

这是你在找什么?http://railscasts.com/episodes/236-omniauth-part-2 :)它显示了如何检索电子邮件等数据.不确定这是不是你想要的.

  • 关闭,我跟着它,但不确定下一步.这是一个很棒的视频,可以帮助任何降落在这里的人,谢谢:) (2认同)