【问题标题】:Saving photos from instagram api calls to Postgresql?将照片从 instagram api 调用保存到 Postgresql?
【发布时间】:2014-09-22 19:23:32
【问题描述】:

所以我想创建一个应用程序,该应用程序向 instagram api 发出 get 请求并获取用户图片提要,同时将照片的 url 自动保存到我的 postgresql 数据库中。我假设这是可能的,但我很难弄清楚如何去做。我的图片提要出来了,但保存到我的数据库是我失败的地方。这是我的 Instagram 控制器:

class InstagramController < ApplicationController 

before_action :set_ig_acc_code, only: [:feed, :search]

def instagram
    # Find the access token
    res = HTTParty.post("https://instagram.com/oauth/access_token/",
     {body:{client_id: ENV["IG_CLIENT_ID"],
      client_secret: ENV["IG_CLIENT_SECRET"],
      grant_type:'authorization_code',
      redirect_uri:'http://localhost:3000/instagram',
      code: params[:code]}})
    redirect_to feed_path(acc_token: res.parsed_response["access_token"])
end

def feed
    # Show a feed of pictures
    if params[:user_id] # For a given user
        @feed = HTTParty.get("https://api.instagram.com/v1/users/" + URI::escape(params[:user_id]) + "/media/recent?access_token=" +
        @acc_token)
    else    # This user's folks they follow
        @feed = HTTParty.get("https://api.instagram.com/v1/users/self/feed?access_token=" +
        @acc_token)
    end
end

def search
    # Search users
    @url = "https://api.instagram.com/v1/users/search?q=" + URI::escape(params[
        :search][:query]) + "&access_token=" +
     params[:acc_token]
    @results = HTTParty.get(@url)
end

private
    def set_ig_acc_code
        @acc_token = params[:acc_token]
    end

end

也许我应该制作一个带有 URL 字段的照片模型,并在它们以某种方式通过我的提要加载时保存它们???我正在用头撞墙。提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby postgresql instagram httparty


    【解决方案1】:

    如果我没记错的话,instagram api 有一个调用来返回关于一个对象的各种数据。尝试在 instagram 开发者网站上搜索媒体 url 等。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      不知道为什么这个问题被否决,因为我认为它是合法的,但这是我发现的一个解决方案,它可能会帮助其他有同样问题的人。我在 InstagramController 中的 Feed 方法的末尾添加了这个:

              @feed["data"].each do |pic|
                  url = pic["images"]["thumbnail"]["url"]
                  Photo.find_or_create_by(url:url)
              end
      

      等待迁移,这很好地保存了 URL。仍在努力保存“标签”数据,但应该是类似的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-22
        • 2018-07-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多