简体   繁体   English

如何转换Facebook Graph API JSON数据以查找数据库中已存在的用户?

[英]How can I convert Facebooks Graph API JSON data to find a user that already exists in my database?

Alright, Iv'e done some research but still no luck... 好吧,我做了一些研究,但还是没有运气...

I'll explain what I'm trying to do first: 我将首先解释我要做的事情:

I have an app which is sign in through Facebook only. 我有一个仅通过Facebook登录的应用程序。 I would like these users to see a list of their friends that are using my application as well. 我希望这些用户也可以看到他们的朋友列表,这些朋友也在使用我的应用程序。

I have routed users#index to return a list of users friends that are currently on the application. 我已经路由users#index返回当前在应用程序上的用户朋友列表。 This is displayed in JSON Data . 这显示在JSON Data This data is returned as follows: "name"=>"Example Name", "id"=>"32938293828393" . 该数据按以下方式返回: "name"=>"Example Name", "id"=>"32938293828393" In my database, I store all users Facebook uid's . 在我的数据库中,我存储了所有用户的Facebook uid's I'd now like to be able to find a user by matching the returned JSON id , by matching it with the uid that iv'e stored in the database. 现在,我希望能够通过匹配返回的JSON id ,通过将其与存储在数据库中的uid进行匹配来查找用户。

This is my current users_controller.rb : 这是我当前的users_controller.rb

def index
   @users = current_user.facebook.get_connections("me", "friends")
   @value = '{"id":"name"}'
   json = JSON.parse(@value).with_indifferent_access
   @users.find(params[json[:id] == :uid]) 
end

and this is my index.html.erb : 这是我的index.html.erb

<%= @users.each do |user| %>
   <%= link_to user.first_name, user %>
<% end %>

Iv'e been trying to do this for the past day with no luck... Done a lot of research. 在过去的一天里,我一直在努力做到这一点,但是运气不好……做了很多研究。 If there is a resource online explaining how to handle this, please drop a link below. 如果在线上有资源说明如何处理,请在下面放置一个链接。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 Thanks :) 谢谢 :)

Found a solution! 找到了解决方案! Will post an answer for anyone else who may need help in the future with a similar problem.. 将为将来可能需要类似问题的其他任何人提供答案。

In my users_controller.rb I now have 在我的users_controller.rb我现在有

class UsersController < ApplicationController
   def index
      @friends = current_user.facebook.get_connections("me", "friends")
      uids = @friends.collect { |f| f["id"] }
      registered_friends = User.where('uid IN (?)', uids)
      @pals = registered_friends
   end
end

And now in my Users/index.html.erb I now have 现在在我的Users/index.html.erb我有了

<% @pals.each do |pal| %>
   <%= link_to pal.first_name, wall_path(pal.id) %>
<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM