Question

I've the following js code to upload image to imgur using Auth.

<script type="text/javascript">
    window.ondragover = function(e) {e.preventDefault()}
    window.ondrop = function(e) {e.preventDefault(); upload(e.dataTransfer.files[0]); }
    function upload(file) {


        if (!file || !file.type.match(/image.*/)) return;


        document.body.className = "uploading";


        var fd = new FormData();
        fd.append("image", file);
        var xhr = new XMLHttpRequest();
        xhr.open("POST", "https://api.imgur.com/3/image.json");
        xhr.onload = function() {
            var code = '[img]' + JSON.parse(xhr.responseText).data.link + '[/img]';
            var editor = eval('opener.' + 'clickableEditor');
            editor.performInsert(code);
            javascript:window.close()
        }

        xhr.setRequestHeader('Authorization', '39-letter-client-secret-code');

        xhr.send(fd);
    }
</script>

It doesn't uploads image to the imgur, but it returns data "undefined". Can you please mention me if I'm missing something here?

Was it helpful?

Solution

I've seen an issue in this line;

xhr.setRequestHeader('Authorization', '39-letter-client-secret-code');

I've changed it to;

xhr.setRequestHeader('Authorization', 'Client-ID 15-letter-Client-ID');

and it works. Sorry to bother you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top