Facebook api POST request to image upload



  • Good day. I can't understend how create a POST request to upload an image. I was searching in the net, and found several versions. But is always response for me (after request) - "(# 324) Requires upload file".
    Thank you!

    ui->webView->load(QUrl("https://www.facebook.com/dialog/oauth?"
                               "client_id=XXXXXXXXXX&"
                               "display=popup&"
                               "response_type=token&"
                               "scope=user_status,user_photos,publish_actions&"
                               "redirect_uri=https://www.facebook.com/connect/login_success.html"));
    

    1

    QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                            accessToken;
    
        QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";
        
        QFileInfo fileInfo(photoPath);
        QFile file(photoPath);
    
        QString comment = "User provided message";
    
        QString bound="---------------------------17673466415141";
    
        QByteArray data;
    
        data.append("--" + bound + "\r\n");
        data.append("Content-Disposition: form-data; name=\"access_token\"\r\n\r\n");
        data.append(accessToken + "\r\n");
    
        data.append("--" + bound + "\r\n");
        data.append("Content-Disposition: form-data; name=\"source\"; filename=\""
                    + file.fileName()+"\"\r\n");
        data.append("Content-Type: image/" + fileInfo.suffix().toLower() + "\r\n\r\n");
        data.append(file.readAll());
        data.append("\r\n");
    
        data.append("--" + bound + "\r\n");
        data.append("Content-Disposition: form-data; name=\"message\"\r\n\r\n");
        data.append(comment);
        data.append("--" + bound + "\r\n");
    
        QNetworkRequest request(uploadUrl);
    
        request.setRawHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        request.setRawHeader("Accept-Language", "en-us,en;q=0.5");
        request.setRawHeader("Accept-Encoding", "gzip,deflate");
        request.setRawHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
        request.setRawHeader("Keep-Alive", "300");
    
        request.setRawHeader("Content-Type",
                             QString("multipart/form-data; boundary=" + bound).toLatin1());
        request.setRawHeader("Content-Length", QString(data.size()).toLatin1());
    
        QNetworkAccessManager * manager = new QNetworkAccessManager(this);
        QNetworkReply * reply =  manager->post(request, data);
        QEventLoop wait;
    
        connect(manager, SIGNAL(finished(QNetworkReply*)), &wait, SLOT(quit()));
        connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));
    
        QTimer::singleShot(10000, &wait, SLOT(quit()));
        wait.exec();
    
        QByteArray answer = reply->readAll();
        reply->deleteLater();
    
        qDebug() << answer;
    

    2

    QString uploadUrl = "https://graph.facebook.com/me/photos?access_token=" +
                            accessToken;
    
        QString photoPath = QCoreApplication::applicationDirPath() + "/image_example.png";
        
        QFileInfo fileInfo(photoPath);
        QFile file(photoPath);
    
        QPixmap pixmap;
        pixmap.load(photoPath);
        QByteArray data;
    
        QBuffer buffer(&data);
        buffer.open(QIODevice::ReadWrite);
        pixmap.save(&buffer, "png");
        buffer.close();
    
        QHttpMultiPart * multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
        QHttpPart imagePart;
        imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/png"));
    
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"source\"; filename=\""+ file.fileName()));
        imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"message\""));
    
        imagePart.setBody(data);
        multiPart->append(imagePart);
    
        QNetworkAccessManager * manager = new QNetworkAccessManager(this);
        QNetworkReply * reply =  manager->post(request, multiPart);
        QEventLoop wait;
    
        connect(manager, SIGNAL(finished(QNetworkReply*)), &wait, SLOT(quit()));
        connect(manager, SIGNAL(finished(QNetworkReply*)), manager, SLOT(deleteLater()));
    
        QTimer::singleShot(10000, &wait, SLOT(quit()));
        wait.exec();
    
        QByteArray answer = reply->readAll();
        reply->deleteLater();
    
        qDebug() << answer;
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.