使用 Imgur API 上传图像时数据无效

Posted

技术标签:

【中文标题】使用 Imgur API 上传图像时数据无效【英文标题】:Invalid data while uploading Image with Imgur API 【发布时间】:2021-10-18 22:10:39 【问题描述】:

我想使用 Imgur API 上传 PNG 图像。当我将测试图像 (png) 转换为 Base64 时,我收到一个错误,指出它不是有效的文件类型。然后我从 Imgur API 文档 (R0lGODlhAQABAIAAAAAAAP//yH5BAEAAAAALAAAABAAEAAAIBRAA7) 中获取测试图像,该文档适用于上传,通过转换器将其转换为“真实”图像,并使用它测试 Base64 转换。但是,我得到了一个不同的 Base64 字符串 (R0lGODlhAQABAOKCrAAAAAAAw7/Dv8O/IcO5BAEAAAAALAAAAAABAAEAAAIBRAA7)。 代码sn-p:

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);

    QUrl url("https://api.imgur.com/3/image");
    QNetworkRequest request(url);

    request.setRawHeader("Authorization", "Client-ID <hereIsTheClientID>");
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QFile image ("C:/Users/pietr/Desktop/image.gif"); // Result from converting example Base64 from Imgur API-Docs - this file can be read by photo viewers
    image.open(QIODevice::ReadOnly);
    QTextStream in (&image);
    QByteArray imageArray = in.readAll().toUtf8().toBase64();
    qDebug() << "Image data:" << imageArray;

    QUrlQuery params;
    params.addQueryItem("image", imageArray);

    connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));

    manager->post(request, params.query().toUtf8());

但是回复是……

    
        "data":
        
            "error":
            
                "code":1003,
                "message":"File type invalid (2)",
                "type":"ImgurException",
                "exception":[],"request":"\/3\/image",
                "method":"POST"
            ,
        "success":false,
        "status":400
    

我在使用 Base64 转换时出错了吗?为什么我从相同的图像中得到不同的值?

【问题讨论】:

【参考方案1】:

图像不是在查询中发送的,而是作为表单数据发送的。使用old answer 并应用于您的情况,解决方案是:

#include <QCoreApplication>
#include <QFile>
#include <QHttpMultiPart>
#include <QNetworkAccessManager>
#include <QNetworkReply>

QHttpMultiPart *buildMultpart(const QVariantMap & data, const QMap<QString, QString> filenames)

    QHttpMultiPart *multipart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
    QVariantMap::const_iterator i_data = data.constBegin();
    while (i_data != data.constEnd()) 
        QHttpPart postpart;
        postpart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(i_data.key()));
        postpart.setBody(i_data.value().toByteArray());
        multipart->append(postpart);
        ++i_data;
    
    QMap<QString, QString>::const_iterator i_filenames = filenames.constBegin();
    while (i_filenames != filenames.constEnd()) 

        QFile *file = new QFile(i_filenames.value());
        if(!file->open(QIODevice::ReadOnly))
            delete  file;
            continue;
        
        QHttpPart postpart;
        postpart.setHeader(QNetworkRequest::ContentDispositionHeader,
                           QString("form-data; name=\"%1\"; filename=\"%2\"")
                           .arg(i_filenames.key(), file->fileName()));
        postpart.setBodyDevice(file);
        multipart->append(postpart);
        file->setParent(multipart);
        ++i_filenames;
    
    return multipart;


int main(int argc, char *argv[])

    QCoreApplication a(argc, argv);

    const QString CLIENT_ID = QLatin1String("clientId");
    const QString API_URL = QLatin1String("https://api.imgur.com");

    QNetworkAccessManager manager;

    QNetworkRequest request;
    QUrl url(API_URL);
    url.setPath("/3/upload");
    request.setUrl(url);
    request.setRawHeader("Authorization", ("Client-ID "+ CLIENT_ID).toUtf8());
    QMap<QString, QString> filenames;
    filenames["image"] = "C:/Users/pietr/Desktop/image.gif";
    QHttpMultiPart *multipart = buildMultpart(, filenames);
    QNetworkReply *reply = manager.post(request, multipart);
    multipart->setParent(reply);
    QObject::connect(reply, &QNetworkReply::finished, [reply]()
        if(reply->error() == QNetworkReply::NoError)
            qDebug().noquote() << reply->readAll();
        
        else
            qDebug() << reply->error() << reply->errorString();
        
        reply->deleteLater();
        QCoreApplication::quit();
    );
    return a.exec();

输出:

"status":200,"success":true,"data":"id":"iT63Heu","deletehash":"JRIdjdWP7lMhzuG","account_id":null,"account_url":null,"ad_type":null,"ad_url":null,"title":null,"description":null,"name":"","type":"image/gif","width":100,"height":75,"size":6166,"views":0,"section":null,"vote":null,"bandwidth":0,"animated":false,"favorite":false,"in_gallery":false,"in_most_viral":false,"has_sound":false,"is_ad":false,"nsfw":null,"link":"https://i.imgur.com/iT63Heu.gif","tags":[],"datetime":1634600948,"mp4":"","hls":""

如果你想使用base64,那么改为:

// ...
request.setRawHeader("Authorization", ("Client-ID "+ CLIENT_ID).toUtf8());
QVariantMap data;
QFile image("C:/Users/pietr/Desktop/image.gif");
if(image.open(QIODevice::ReadOnly))
    data["image"] = image.readAll().toBase64();
    data["type"] = "base64";
    image.close();

QHttpMultiPart *multipart = buildMultpart(data, );
QNetworkReply *reply = manager.post(request, multipart);
// ...

【讨论】:

没有名为“buildMultpart”的函数。 @Bamp See QHttpMultiPart *buildMultpart(const QVariantMap & data, const QMap 文件名) 我用的是Qt5,没有这样的会员。 @Bamp 我已经创建了它,它在我答案的第一个代码中。 哦,我忽略了它......但它仍然不起作用。回复者:\"data\":\"error\":\"Invalid URL (--boundary_.oOo._WYpbiR+sSXxyfEAzLx8X3Dq6QRYTX2cM\\r\\nContent-Disposition: form-data; name=\\\"image\\\"\\r\\n\\r\\nR0lGODlhEAL3AHAA [here's more and more of this unreadable stuff] cJoUvYtJkwSxjzGYkAAADs=\\r\\n--boundary_.oOo._WYpbiR+sSXxyfEAzLx8X3Dq6QRYTX2cM\\r\\nContent-Disposition: form-data; name=\\\"type\\\"\\r\\n\\r\\nbase64\\r\\n--boundary_.oOo._WYpbiR+sSXxyfEAzLx8X3Dq6QRYTX2cM--\\r\\n)\",\"request\":\"\\/3\\/image\",\"method\":\"POST\",\"success\":false,\"status\":400

以上是关于使用 Imgur API 上传图像时数据无效的主要内容,如果未能解决你的问题,请参考以下文章

即使 url 有效,使用 imgur api 获取损坏的图像

上传到 Cloudinary API - 文件参数无效

从 Mathematica 上传图像到 Imgur

cloudinary -base 64 图像上传给出无效的 JSON 响应

用于将图像从 Imgur 拉到不和谐机器人中的 API

Imgur API GET 图像问题