代码之家  ›  专栏  ›  技术社区  ›  Designer

如何从Twitter API获取Twitter用户网站数据,以便我的Rails设备注册?

  •  0
  • Designer  · 技术社区  · 8 年前

    我正在将Omniauth Twitter与设备一起使用。

    推特API给了我

    {
      "id": 2244994945,
      "id_str": "2244994945",
      "name": "TwitterDev",
      "screen_name": "TwitterDev",
      "location": "Internet",
      "profile_location": null,
      "description": "Developer and Platform Relations @Twitter. We are developer advocates. We can't answer all your questions, but we listen to all of them!",
      "url": "http://twitter.com",
      "entities": {
        "url": {
          "urls": [
            {
              "url": "https://twitter.com",
              "expanded_url": "https://dev.twitter.com/",
              "display_url": "dev.twitter.com",
              "indices": [
                0,
                23
              ]
            }
          ]
        },
        "description": {
          "urls": []
        }
      },
    .....
    

    我需要得到 扩展的\u url 对于我的网站字段,但它是一个数组,我如何获得这些数据?

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create do |designer|
          user.provider = auth.provider
          user.uid = auth.uid
    
          user.username = auth.info.nickname
    
          user.website = HERE_I_NEED_USERS_WEBISTE_INFO
        end
      end
    
    2 回复  |  直到 8 年前
        1
  •  0
  •   Taryn East    8 年前

    假设您在一个名为 twitter_data . 如果它具有上述结构,您将能够访问 extended_url 这种方式:

    twitter_data["entities"]["url"]["urls"].first["expanded_url"]
    

    我建议您在控制台中尝试使用一些真实数据,看看您得到了什么。

    编辑:

    根据 omniauth-twitter gem doco,你可以从 authentication hash .

    看起来您可以获得如下相关url:

    auth = request.env['omniauth.auth']
    twitter_data = auth[:extra][:raw_info]
    twitter_data["entities"]["url"]["urls"].first["expanded_url"]
    
        2
  •  0
  •   Designer    8 年前

    将此添加到我的设计器。rb成功了!

      begin 
        user.website = Net::HTTP.get_response(URI.parse(auth.info.urls.Website))['location']
        rescue
          user.website = auth.info.urls.Website
      end