让 OmniAuth、Devise 和 Koala 协同工作 [英] Making OmniAuth, Devise and Koala work together
问题描述
我有一个应用程序,我在其中使用 devise 和 omniauth 实施身份验证.我已经弄清楚了登录/退出等问题,我想知道的是,与用户图 api 端点的连接初始化并准备在我的应用程序中使用的最有效方法是什么.
I have an app in which I am implementing authentication using devise and omniauth. Ive got the logging in / out etc figured out, what i wanted to know was, what is the most efficient way to have a connection to the users graph api end point initialised and ready for use within my application.
例如:如果我想在个人资料页面上做,
eg: If on the profile page i wanted to do,
profile_image = current_user.fbgraph.get_picture("me")
我将如何用最少的 API 调用来实现这一点(我将在整个应用程序中使用类似的调用)
How would I accomplish this with the least number of API calls (i will be using similar calls throughout the application)
推荐答案
你可以使用类似Koala一>.当您对用户进行身份验证时,您可以获取访问令牌.假设您已遵循 Devise/Omniauth 教程,您可以做一些事情像这样:
You can accomplish this using something like Koala. When you authenticate the user, you can grab the access token. Assuming you've followed the Devise/Omniauth tutorial, you could do something like so:
def self.find_for_facebook_oauth(response, signed_in_resource=nil)
data = response['extra']['user_hash']
access_token = response['credentials']['token']
user = User.find_by_email(data["email"])
# only log in confirmed users
# that way users can't spoof accounts
if user and user.confirmed?
user.update_attribute('fb_access_token', access_token)
user
end
end
获得访问令牌后,您可以执行以下操作:
Once you have the access token, you could then do something like:
@graph = Koala::Facebook::API.new(@user.fb_access_token)
profile_image = @graph.get_picture("me")
在我的应用程序中,当来自 Facebook 的回调到来时,我会检查用户是否已登录.如果是,我假设请求是链接帐户.如果不是,我认为这是一个登录请求.
In my app, I check to see if a user is logged in when the callback from Facebook comes. If they are, I assume the request was to link accounts. If they're not, I assume it's a login request.
这篇关于让 OmniAuth、Devise 和 Koala 协同工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!