Imgur api responding with code 403 with server error 429
Asked Answered
K

1

7

I'm trying to upload a image using html form with imgur api(react).

I've selected OAuth 2 authorization with a callback URL when registering api.

The problem is that api is wont work with error 429 (sometimes net::ERR_HTTP2_PROTOCOL_ERROR).

This is the code

const imageUpload = (e) => {
    console.log("called");
    var fileIn = e.target;
    var file = fileIn.files[0];
    if (file && file.size < 5e6) {
        const formData = new FormData();

        formData.append("image", file);
        fetch("https://api.imgur.com/3/image", {
            method: "POST",
            headers: {
                Authorization: "Client-ID //my client Id",
                Accept: "application/json",
            },
            body: formData,
        })
            .then((response) => response.json())
            .then((response) => {
                e.preventDefault();
                console.log(response);
                console.log(response.data.link);
                url_in = response.data.link;
            });
    } else {
        console.error("oversized file");
    }
}

This is input tag code

<input type="file" name="image" id="upload" onChange={imageUpload}></input>

I just need the url of the uploaded image

Kean answered 14/2, 2021 at 11:47 Comment(3)
I've been going crazy tonight trying to figure something similar out. I'm trying to run a simple search from a react app running locally and I am only getting 429 errors. If I hit the same url with curl, it works fine and returns results.Metrical
Solution for WSL users -> #66617282Apia
Here's my solution for the WSL users -> #66617282Apia
M
7

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

Metrical answered 15/2, 2021 at 14:28 Comment(4)
Thx for the advice! I've tried this and it still hosts in localhost:3000. I'm not used to react... Can you explain more about the host settings? (Tried setting HOST=0.0.0.0 in .env file)Kean
I think the host option passed to react-scripts just tells it to make sure things are accessible at that address. 0.0.0.0 just refers to your own machine anyways. react-scripts will still auto opens the browser to localhost:3000, but if you change the url to 0.0.0.0:3000 you should see your page. I think imgur inspects the request header and just auto rejects anything coming from localhost.Metrical
Thx! This worked for me. Hope you have a wonderful dayKean
I have found that opening up the command prompt on windows and typing ipconfig will reveal your local ip. Then open your browser and type that ip with :3000 so it will look like this 124.169.2.137:3000 (not my actual ip just an example). Imgur seems to want to block localhost so running it from something else seems to work.Anile

© 2022 - 2024 — McMap. All rights reserved.