OmniAuth + Pulling Tweets, FB Places, etc

北城余情 提交于 2019-11-28 17:20:33

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.

Is this what you're looking for? http://railscasts.com/episodes/236-omniauth-part-2 :) It shows how to retrieve data such as email. Not sure if that's what you're looking for.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!