让 OmniAuth、Devise 和 Koala 协同工作

发布于 2024-11-18 08:52:12 字数 272 浏览 4 评论 0原文

我有一个应用程序,在其中使用 devise 和omniauth 实现身份验证。我已经弄清楚了登录/注销等问题,我想知道的是,连接到用户图 api 端点的最有效方法是什么?初始化并准备在我的应用程序中使用。

例如:如果在个人资料页面上我想要执行此操作,

profile_image = current_user.fbgraph.get_picture("me")

我将如何用最少数量的 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")

How would I accomplish this with the least number of API calls (i will be using similar calls throughout the application)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瑶 2024-11-25 08:52:12

您可以使用 Koala 之类的东西来完成此操作。当您对用户进行身份验证时,您可以获取访问令牌。假设您已遵循 Devise/Omniauth 教程,您可以做一些事情像这样:

  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

一旦您拥有访问令牌,您就可以执行以下操作:

@graph = Koala::Facebook::API.new(@user.fb_access_token)
profile_image = @graph.get_picture("me")

在我的应用程序中,当来自 Facebook 的回调到来时,我检查用户是否已登录。如果是,我认为请求是链接帐户。如果不是,我认为这是一个登录请求。

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")

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文