【发布时间】: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