【问题标题】:How do I get a user's location using Koala in rails?如何在 Rails 中使用 Koala 获取用户的位置?
【发布时间】:2011-08-17 20:42:45
【问题描述】:

我的会话控制器中有以下代码,它将 Facebook 朋友、喜欢和用户个人资料等信息保存到我的数据库中。个人资料包括用户的位置和性别,但它作为字符串保存到数据库中,因此我无法提取它。

@graph = Koala::Facebook::GraphAPI.new(current_user.token)
current_user.profile = @graph.get_object("me")
current_user.likes = @graph.get_connections("me", "likes")
current_user.friends = @graph.get_connections("me", "friends")
current_user.save

进入控制台,我可以通过以下方式获取最后一个用户的个人资料:

u = User.last.profile

但这并不能让我专门调用该位置,例如:

User.last.profile.location

用户表的样子

create_table "users", :force => true do |t|
t.string   "provider"
t.string   "uid"
t.string   "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "token"
t.string   "likes"
t.string   "profile"
t.string   "location"
t.string   "interests"
t.string   "birthday"
t.string   "activities"
t.string   "friends"
end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 facebook-graph-api koala


    【解决方案1】:

    喜欢这样吗?

    graph = Koala::Facebook::API.new(@oauth_token)
    @user = graph.get_object("me")
    location = @user["location"]["name"]
    

    【讨论】:

      【解决方案2】:

      我正在使用koala v2.2,而我的应用程序 Facebook API 版本是 v2.4。 我只使用get_object("me") 请求没有得到location 信息。我必须将location 作为fields 参数传递,例如

      get_object("me?fields=first_name,last_name,location")
      

      这些信息可能会对某人有所帮助。

      N.B:当然你必须启用user_location 范围。

      【讨论】:

      • 确实非常有帮助,谢谢!似乎 fb 一直在更改它的 API。非常令人沮丧。
      【解决方案3】:

      我用来解决这个问题的方法是使用范围并在视图中调用这些范围,这可能不是最好的方法,但可以快速解决。

      用户模型的范围可以通过:

      scope :CityName, where("profile like '%location: CityName%'")
      

      【讨论】:

        【解决方案4】:

        从昨天开始使用 Facebook Checkins,例如:

        unless graph.get_connections('me', 'checkins', :since => 'yesterday').blank?
        checkin = graph.get_connections('me', "checkins", :since => 'yesterday')
        lat = checkin[0]['place']['location']['latitude']
        long = checkin[0]['place']['location']['longitude']
        

        【讨论】:

        • 问题是关于用户个人资料中的位置,而不是他们的签到,但这可能对将来可能需要它的任何人有用。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-06
        • 2014-01-26
        • 2014-08-06
        • 1970-01-01
        相关资源
        最近更新 更多