问题描述
我有一个应用程序,我正在使用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)
推荐答案
您可以使用类似。当您验证用户时,您可以抓取访问令牌。假设您遵循,您可以做一些事情像这样:
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一起工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!