next.js 从GET请求Imgur API接收错误429

cu6pst1q  于 2023-10-18  发布在  其他
关注(0)|答案(1)|浏览(125)

我从Imgur API得到一个Request failed with status code 429,而我的Client_ID是新的,我甚至没有使用过它,
以下是我的API.ts:

const imgurClientId = process.env.NEXT_PUBLIC_Client_ID

const BASE = "https://api.imgur.com/3";
const EP_GALLERY = `${BASE}/gallery`;
const EP_GALLERY_TAGS = `${BASE}/tags`;

 async function imgurBaseApi(args: Args) {
    const myHeaders = new Headers({
      Authorization: `Client-ID ${imgurClientId}`,
    });

    args.requestOptions = {
      ...args.requestOptions,
      headers: myHeaders,
      method: "GET",
    };

    const response = await fetch(args.endPoint, args.requestOptions);
    const responseJson = await response.json();

    return responseJson.data;
  }

// how I use it:
  async function getGalleryTagMetadata(requestArgs: TypeState["requestArgs"]) {
    const endPoint = `${EP_GALLERY}/t/${requestArgs.tagName}/${requestArgs.sort}/${requestArgs.window}/${requestArgs.page}`;

    return imgurBaseApi({ endPoint: endPoint });
  }

我也尝试了简单的fetch调用,如下所示:

const imgurClientId = process.env.NEXT_PUBLIC_Client_ID

  const sort = 'viral'
  const window = 'day'
  const page = 1

  const url = `https://api.imgur.com/3/gallery/search/${sort}/${window}/${page}?q=cats`

  const requestOptions = {
    method: 'GET',
    headers: new Headers({
      Authorization: `Client-ID ${imgurClientId}`,
    }),
  }

  fetch(url, requestOptions)
    .then((response) => {
      if (!response.ok) {
        throw new Error(`HTTP error! Status: ${response.status}`)
      }
      return response.json()
    })
    .then((data) => {
      console.log('dta', data)
    })
    .catch((error) => {
      console.error('Error:', error)
    })

我已经按照Imgur文档设置了我的Client_ID,当我用Postman尝试它时,它工作得很好,成功地请求了GET
我的问题是:
由于这是一个应用程序只是为了接收一些图像,我需要通过0auth2步骤吗?
ps.完整的代码更长,但让我知道,我会在这里打印出来
Thanks in advance
欢呼

daupos2t

daupos2t1#

我将package.json中的启动脚本更改为“start”:“react-scripts start --host 0.0.0.0“基于这里提到的注解someone。然后我把浏览器指向http://0.0.0.0:3000/,我就能从imgur得到回应。

相关问题