我无法使用JSON中的Instagram图像的URL

发布于 2025-02-04 10:38:03 字数 3457 浏览 3 评论 0 原文

我正在开发一个API,以从特定的主题标签中获取数据。

此JSON它以不同的分辨率返回图像,但它是一个错误的URL,我无法使用此图像,我不知道如何解决该问题。

url: https://wwwww.instagram.com/explore.com/explore/xplore/tags/jeep/jeep/jeep /?__ a = 1

错误:get https://scontent-gru1-1.cdninstagram.com/v/t51.2885-15/284660975_5107249660606061398_8181818596589777777183559999_JPGIN.JPGIN.JPTICTING 40_SH0.08& _nc_ht = scontent-gru1-1.cdninstagram。 com&_nc_cat=110&_nc_ohc=DOEOKz-tf2UAX98qbHQ&edm=AABBvjUBAAAA&ccb=7-5&oh=00_AT-Im7KGjYftnZu6Am-_FV7RKW7CVDcYmyzhU1zB0vKCvg&oe=629F3ED8&_nc_sid=83d603 net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin 200

Index. JS

const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");

// Routes
const routeLogin = require('./routes/login');
const routeRegister = require('./routes/register');
const routeHashtags = require('./routes/hashtag');

const app = express();
const cors = require("cors");



// Use
app.use(cors())

// Morgan
app.use(morgan("dev"));

// Body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());



// Routes
app.use('/login', routeLogin);
app.use('/register', routeRegister);
app.use('/hashtag', routeHashtags);

// Error
app.use((req, res, next) => {
  const error = new Error("Erro! Não encontrado!");
  error.status = 404;
  next(error);
})

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  return res.send({
    error: {
      message: error.message
    }
  })
})



module.exports = app;

我如何获取数据

async function dashboard(){
  async function pendingPosts(){
    let divPendImages = document.getElementById("pendingPosts_images");
    let ctaNo = document.getElementById("pendingPosts_cta_no")
    let ctaYes = document.getElementById("pendingPosts_cta_yes")

    // Get data
    const res = await fetch("http://localhost:3120/hashtag/")
                      .then((res) => res.json())
                      .then((data) => {return data.response})

    // Show data
    function showData(){
      console.log(res.length)

      res.forEach((element, index) => {
        // Img
        let img = document.createElement("img")
        let src = element.node.thumbnail_src;
        img.src = src;

        ctaNo.parentNode.querySelector("input").value = src;
        ctaYes.parentNode.querySelector("input").value = src;

        // CTA
        if(index == 0){
          ctaNo.parentNode.action = `http://localhost:3120/hashtag/notadd/${element.node.id}`;
          ctaYes.parentNode.action = `http://localhost:3120/hashtag/create/${element.node.id}`;
        }

        // Append img
        divPendImages.appendChild(img);
      });
    }
    showData()
  }
  pendingPosts()
}
dashboard()

I'm developing an api to get data from a specific hashtag.

This json it returns the image in different resolutions, but it's a url that gives an error, I can't use this image and I don't know how to solve the problem.

URL: https://www.instagram.com/explore/tags/jeep/?__a=1

Error: GET https://scontent-gru1-1.cdninstagram.com/v/t51.2885-15/284660975_5107249636061398_8185965897577183599_n.jpg?stp=dst-jpg_e35_p640x640_sh0.08&_nc_ht=scontent-gru1-1.cdninstagram.com&_nc_cat=110&_nc_ohc=DOEOKz-tf2UAX98qbHQ&edm=AABBvjUBAAAA&ccb=7-5&oh=00_AT-Im7KGjYftnZu6Am-_FV7RKW7CVDcYmyzhU1zB0vKCvg&oe=629F3ED8&_nc_sid=83d603 net::ERR_BLOCKED_BY_RESPONSE.NotSameOrigin 200

Index.js

const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");

// Routes
const routeLogin = require('./routes/login');
const routeRegister = require('./routes/register');
const routeHashtags = require('./routes/hashtag');

const app = express();
const cors = require("cors");



// Use
app.use(cors())

// Morgan
app.use(morgan("dev"));

// Body parser
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());



// Routes
app.use('/login', routeLogin);
app.use('/register', routeRegister);
app.use('/hashtag', routeHashtags);

// Error
app.use((req, res, next) => {
  const error = new Error("Erro! Não encontrado!");
  error.status = 404;
  next(error);
})

app.use((error, req, res, next) => {
  res.status(error.status || 500);
  return res.send({
    error: {
      message: error.message
    }
  })
})



module.exports = app;

How am I getting the data

async function dashboard(){
  async function pendingPosts(){
    let divPendImages = document.getElementById("pendingPosts_images");
    let ctaNo = document.getElementById("pendingPosts_cta_no")
    let ctaYes = document.getElementById("pendingPosts_cta_yes")

    // Get data
    const res = await fetch("http://localhost:3120/hashtag/")
                      .then((res) => res.json())
                      .then((data) => {return data.response})

    // Show data
    function showData(){
      console.log(res.length)

      res.forEach((element, index) => {
        // Img
        let img = document.createElement("img")
        let src = element.node.thumbnail_src;
        img.src = src;

        ctaNo.parentNode.querySelector("input").value = src;
        ctaYes.parentNode.querySelector("input").value = src;

        // CTA
        if(index == 0){
          ctaNo.parentNode.action = `http://localhost:3120/hashtag/notadd/${element.node.id}`;
          ctaYes.parentNode.action = `http://localhost:3120/hashtag/create/${element.node.id}`;
        }

        // Append img
        divPendImages.appendChild(img);
      });
    }
    showData()
  }
  pendingPosts()
}
dashboard()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2025-02-11 10:38:03

想象一下,如果某些受欢迎的社交媒体人员发布了指向 小松体托管website.com 的链接,该链接每秒只能处理10个请求。如果该人足够受欢迎,他们可能会有10个以上的关注者遵循他们的链接并删除小型网站。这可能是恶意的(例如,社交媒体人对该网站怀有怨恨)或无意的(社交媒体人都不会理解服务器的工作原理)。

因此,像Chrome这样的浏览器允许服务器/站点添加提示是否要接受其他起源的请求。尽管在HTTPS框架中进行了标准化,但这完全是可选的,只是提示。即,这取决于客户端(例如Web浏览器)来强制执行该策略,而不是服务器(因为这会违反最小化服务器流量的目的)。

解决此问题的方法是:

  1. 与服务器联系(在您的情况下为Instagram),并询问他们是否有意。如果这是公共API,并且他们打算让任何人能够消耗这些URL,那么他们可能会偶然地忘记删除Cross Orgins限制

  2. 使用不尊重交叉原产性策略的客户端。例如 curl electron ,而独立应用程序不会执行交叉起源策略。大多数浏览器可能也有禁用其执行的标志。

查看 mdn> mdn 或搜索'cors'资源共享)用于更多背景。

Imagine if some popular social media person posts a link to small-locally-hosted-website.com that can only handle 10 requests per second. If the person's popular enough, they might have more than 10 followers follow their link and take down the the small website. This could be malicious (e.g. the social media person has a grudge against the site) or unintentional (the social media person is not expected to understand how servers work).

So browsers like chrome allow servers/sites to add a hint whether they want to accept requests from other origins. Though standardized in the https framework, this is totally optional and just a hint. I.e., it's up to clients (e.g. web browsers) to enforce the policy, and not the server (as that would defeat the purpose of minimizing server traffic).

The way around this is either:

  1. Contact the server (instagram in your case) and ask them if this is intentional. If it's a public API and they intend for these URLs to be able to be consumed by anyone, it may be accidentally they forgot to remove the cross orgins restrictions

  2. Use a client that doesn't respect the cross origin policy. E.g. curl, electron, and standalone apps don't enforce the cross origins policy. Most browsers probably have flags that would disable their enforcement as well.

Look at MDN or search for 'CORS' (Cross origin resource sharing) for more background.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文