Я зарегистрировал приложение на imgur.com (для анонимного использования) и получил ключ приложения. Я использую его здесь:
self.uploadImage = function(file) {
/* Is the file an image? */
if (!file || !file.type.match(/image.*/)) return;
/* It is! */
document.body.className = "uploading";
/* Lets build a FormData object*/
var fd = new FormData(); // I wrote about it: https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
fd.append("image", file); // Append the file
fd.append("key", "<my key>"); // Get your own key http://api.imgur.com/
var xhr = new XMLHttpRequest(); // Create the XHR (Cross-Domain XHR FTW!!!) Thank you sooooo much imgur.com
xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
xhr.onload = function() {
// reference side-specific class here
document.querySelector("#image-uploaded-one-" + self.cardId()).href = JSON.parse(xhr.responseText).upload.links.imgur_page;
}
// Ok, I don't handle the errors. An exercice for the reader.
/* And now, we send the formdata */
xhr.send(fd);
};
Если я использую свой ключ, я получаю сообщение об ошибке Cannot read property 'links' of undefined
, однако, если я использую тот, который я нашел в учебнике, все работает так, как ожидалось. Я создал ключ несколько дней назад, поэтому не думаю, что проблема во времени. Что еще это может быть?
Я думаю, проблема в том, что ключ, который работает, был сгенерирован v2 API, а новые - v3, которые не будут работать с указанным v2. Если я укажу v3, я получаю «HTTP-доступ отключен. Запросы должны использовать ssl». Как я могу заставить это работать?