Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

ruby on rails - OmniAuth + Pulling Tweets, FB Places, etc

I'm using OmniAuth + Devise to allow users to register using Facebook/Twitter/Gowalla/etc attached to normal user accounts. Now when a user logs in using any of these, or their account, all their social networks are attached in a authentications table.

I need to be able to pull content from any of these providers, such as their tweets or their Facebook Places checkings, etc. I understand that I will need to use a different gem, plugin, whatever to do this but getting the config I need to work with those gems (and make requests) is confusing to me.

I need to be able to access the provider config items in omniauth.rb so I have API Keys and Secret Keys, etc, then I need to be able to grab tokens from the oAuth stuff to make requests.

Other gems like https://github.com/jrallison/authlogic_oauth seem to store oauth_token, oauth_secret and oauth_token, but OmniAuth does not.

As you can probably tell I am very new to Ruby, Rails and oAuth, so this is turning out to be a very challenging application. Help is very much needed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sorted!

Added token and secret to the authentications table described in http://railscasts.com/episodes/236-omniauth-part-2 but changed the authentication.build line to take two more parameters:

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

then used the code example from 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

By pulling the content from current_user.authentications (im finding the first as they should only have one) I can grab the token and security and its alllll good.

Now I can tweak this, get stuff saved, faff with the JSON and take what I need. I'm sure Facebook will be pretty similar.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...