简体   繁体   English

如何在轨道上使用 ruby 获取 Instagram 照片?

[英]how to get instagram photos with ruby on rails?

I am trying to show Instagram photos in my Ruby on Rails application.我正在尝试在我的 Ruby on Rails 应用程序中显示 Instagram 照片。 The following code works on my local machine but on my server in AWS it does not show the images.以下代码适用于我的本地计算机,但在我的 AWS 服务器上它不显示图像。

At the time of making data = JSON.parse file.read does not seem to return any value on the server, but on my PC it shows the JSON .在制作data = JSON.parse file.read似乎没有在服务器上返回任何值,但在我的电脑上它显示JSON What could I be missing?我会错过什么?

<%
require "open-uri"
photo_count = 10
begin
  file = open("https://www.instagram.com/#{@user.instagram}/?__a=1")
  data = JSON.parse file.read
%>
  <% data['graphql']['user']['edge_owner_to_timeline_media']['edges'].each do |instagram| %>
    <div class="col-xl-6 xd">
      <!-- Photo Box -->
      <% @caption = instagram['node']['edge_media_to_caption']['edges'].count > 0 ? instagram['node']['edge_media_to_caption']['edges'][0]['node']['text'] : "" %>
      <a href="<%=instagram['node']['display_url']%>" class="photo-box mfp-gallery" data-background-image="<%=instagram['node']['display_url']%>" data-toggle="lightbox" title="Instagram">
        <div class="photo-box-content">
          <span>Instagram</span>
        </div>
      </a>
    </div>
  <% end %>
<% rescue => ex %>
<% end %>

Update:更新:

When doing file.read on my local machine I can see that it shows me all the instagram user data in this way:在我的本地机器上执行 file.read 时,我可以看到它以这种方式向我显示所有 instagram 用户数据:

{"logging_page_id":"profilePage_31229027","show_suggested_profiles":false,"show_follow_dialog":false,"graphql":{"user":{"biography":"Inquiries to email@gmail.com\nAll images are under artist copyrights","blocked_by_viewer":false,"country_block":false,"external_url":"http://www.website.com/","external_url_linkshimmed":"https://l.instagram.com/?u=http%3A%2F%2Fwww.wrl ...

but on the server I get this:但在服务器上我得到了这个:

<!DOCTYPE html> <html lang="en" class="no-js not-logged-in client-root"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title> Login • Instagram </title> <meta name="robots" content="noimageindex, noarchive"> <meta name="apple-mobile-web-app-status-bar-style" content="default"> <meta name="mobile-web-app-capable" content="yes"> <meta name="theme-color" content="#ffffff"> <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, viewport-fit=cover"> <link rel="manifest" href="/data/manifest.json">

what could be wrong?有什么问题?

If you are making a lot of requests, maybe Instagram has blocked your server to get images, your source is working, but the Instagram API has protection.如果您发出大量请求,可能 Instagram 已阻止您的服务器获取图像,您的来源正在工作,但 Instagram API 有保护。

You can try to use an IPs proxy to get the images.您可以尝试使用 IP 代理来获取图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM