Receive error429 from GET request Imgur API

108 Views Asked by At

I am getting a Request failed with status code 429 from Imgur API while my Client_ID is new and I haven't even used it,

here is my 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 });
  }

I also tried it with simple fetch call like this:



  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)
    })

I have followed the Imgur documentation to set up my Client_ID and when I tried it with Postman it worked fine with a successful GET request.

My question:

As this is an application just to receive some images, do I need to go through the 0auth2 steps?

ps. The complete code is longer, but let me know and I will print it here

Thanks in advance

cheers enter image description here

1

There are 1 best solutions below

1
peakyBlinders On

I changed my start script in package.json to this "start": "react-scripts start --host 0.0.0.0" based on a comment someone mentioned here. Then I point my browser at http://0.0.0.0:3000/ and I'm able to get a response from imgur.