【问题标题】:Make Second api call with attributes already obtained - Facebook Api使用已获得的属性进行第二次 api 调用 - Facebook Api
【发布时间】:2014-04-30 21:48:03
【问题描述】:

我不确定如何使用 facebook 处理我的 api 调用的下一部分。目前我正在使用用户 ID 检索所有帖子

"https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN}"

但我也想通过使用从第一个 api 调用返回的 object_id 来获取与每个帖子关联的图片

"https://graph.facebook.com/#{object_id}/picture

所以目前我有一个类来处理第一个 api 调用,然后将返回的数据保存到我的模型中

class FacebookFeed
#Constants
VANDALS_ID = ENV['VANDALS_FB_ID']
FB_ACCESS_TOKEN = ENV['FACEBOOK_ACCESS_TOKEN']
FACEBOOK_URL = "https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN}"

def get_feed
  uri = URI(FACEBOOK_URL)
  response = HTTParty.get(uri)
  results = JSON.parse(response.body)
  puts formatted_data(results)
end

def formatted_data(results)

 return unless results

    results['data'].map { |m| 
    attrs = { message: m['message'], 
              picture: m['picture'], 
              link: m['link'], 
              object_id: m['object_id']
      }.compact 


     Post.where(attrs).first_or_create! do |post|
        post.attributes = attrs
     end
   }
   end

  def get_large_photo(object_id)
  #added this to handle the second api request and pass through the object_id

  end

所以我从第一个 api 调用中想要的所有信息都在一个名为 attrs 的哈希中,然后我可以通过 attrs['object_id'] 访问 object_id 吗?那是正确的是吗? .

我坚持如何从哈希中获取每个 object_id 并在单独的调用中获取与其关联的图片,然后保存分配给键的值 :picture 并将其放回我的 attrs 哈希中,以便我可以将所有内容保存在我的帖子模型中

我希望这是有道理的

任何帮助表示赞赏

【问题讨论】:

    标签: ruby-on-rails ruby facebook-graph-api hash


    【解决方案1】:

    我会做这样的事情,尽管我仍然对你想要实现的目标感到困惑:

    def formatted_data(results)
        if results[:data]
            for record in results[:data] do
                 attrs = {
                     message: record[:message],
                     picture: record[:picture],
                     link: record[:link],
                     object_id: record[:object_id]
                 }
                 Post.where(attrs).first_or_create!(attrs)
    
                 #perform second call
                 if record[:object_id]
                    post = Post.find_by object_id: record[:object_id] 
                    if post
                        fb_large_picture_url = get_large_photo(record[:object_id])
                        post.update_attribute(large_image_url: fb_large_picture_url)
                    end
                 end
            end
        end
     end
    
     def get_large_photo(object_id)
         uri = URI("https://graph.facebook.com/#{object_id}/picture")
         response = HTTParty.get(uri)
         results = JSON.parse(response.body)
         return formatted_picture_data(results) #new method to handle response from second api call
     end
    

    在 Skype 上与 Rich 讨论后,似乎真正的问题是他试图修改数据库以使用 object_id 记录以再次调用 API,发布更大的图像

    这效率很低,但仍然可以工作

    【讨论】:

      【解决方案2】:

      有一个很棒的与 FB API 交互的宝石 - koala。它在 github 上有非常好的 Wiki 页面。

      有了这个 gem,很容易获得你需要的所有信息。 您可以使用graph.get_connections 或graph.get_object 来获得您想要的。

      这是一个例子:

      gem 'koala', "~> 1.8.0rc1"
      require 'koala'
      require 'pp'
      
      user_access_token = 'access_token_here'
      graph = Koala::Facebook::API.new(user_access_token) 
      
      pp books_raw = graph.get_connections('me', 'books') 
      # pp likes_raw = graph.get_connection('me', 'likes')
      
      books_ids_array, books_names_array = [], [] 
      
      books_raw.each_with_index {|element, i|
        puts "id: #{books_raw[i]['id']} \n\t name: #{books_raw[i]['name']}"
        books_ids_array << books_raw[i]['id'] 
        books_names_array << books_raw[i]['name']
      }
      
      books_ids_array.each {|node|
        pp book_page = graph.get_object(node)
      }
      puts 
      

      【讨论】:

        【解决方案3】:

        非常感谢 Rich Peck 的帮助,在聊天(几个小时 :-))并了解了所有关于 Rails.logger.info()、return 和许多其他事情之后,我们有一个可行的解决方案,尽管不是最有效,但很有效。

        这就是类现在的样子,如果有人有任何重构技巧,那么将不胜感激

        require 'httparty'
        require 'open-uri'
        require 'json'
        
        class FacebookFeed
        #Constants
        VANDALS_ID = ENV['VANDALS_FB_ID']
        FB_ACCESS_TOKEN = ENV['FACEBOOK_ACCESS_TOKEN']
        FACEBOOK_URL = "https://graph.facebook.com/#{VANDALS_ID}/posts/?access_token=#{FB_ACCESS_TOKEN}"
        
        def get_feed
          uri = URI(FACEBOOK_URL)
          response = HTTParty.get(uri)
          results = JSON.parse(response.body)
          return formatted_data(results)  
        end
        
        def formatted_data(results)
          if results['data']
            for record in results['data'] do
                 attrs = {
                     message: record['message'],
                     picture: record['picture'],
                     link: record['link'],
                     object_id: record['object_id']
                 }
        
                 Post.where(attrs).first_or_create! do |post|
                  post.attributes = attrs
                 end
        
                 #perform second call
                 if record['object_id']
                    post = Post.find_by object_id: record['object_id']
                    if post
                        fb_large_picture_url = get_large_photo(record['object_id'])
                        post.update_column(:large_image_url, fb_large_picture_url)
                    end
                 end
              end
           end
        end
        
         def get_large_photo(object_id)
           second_uri = URI("https://graph.facebook.com/#{object_id}/?picture")
           second_response = HTTParty.get(second_uri)
           second_results = JSON.parse(second_response)
           return formatted_picture_data(second_results)
         end
        
         def formatted_picture_data(second_results)
           return second_results['source']
         end
        
        end
        

        【讨论】:

          猜你喜欢
          • 2020-04-13
          • 1970-01-01
          • 1970-01-01
          • 2019-11-30
          • 1970-01-01
          • 1970-01-01
          • 2019-12-09
          • 2017-05-20
          相关资源
          最近更新 更多