排序!
在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
通过从 current_user.authentications 中提取内容(我找到了第一个,因为他们应该只有一个),我可以获取令牌和安全性,而且一切都很好。
现在我可以调整它,保存东西,使用 JSON 并获取我需要的东西。我相信 Facebook 会非常相似。