PDA

View Full Version : curl command to ssl handshake and https posts



k1447321
7th September 2015, 22:49
hey there,

I would like to transform a curl request to a qt method curl command looks like this:
curl --cacert cacert.pem --request 'POST' 'https://api.twitter.com/1.1/statuses/update.json' --data 'status=ekmek' --header 'Authorization: OAuth oauth_consumer_key="ir0j0XHEZAjl5OIMLvRtGDpvz", oauth_nonce="d0d616f462d9d3dd5bb39b23b2ae76ea", oauth_signature="2IAkMds2Pxtp699MReoyupgfZl8%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1441658718", oauth_token="3427524377-klG6CAjAWyWOo9eR7g6WtRm8nGoo9CtQHSX3jpC", oauth_version="1.0"' --verbose


so I want to have a ssl handshake using my cacert.pem and send my request with a body and a custom header.

thanks in advance :)

ChrisW67
11th September 2015, 11:07
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QDebug>
#include <QSslConfiguration>

class Fetch: public QObject
{
Q_OBJECT
public:
Fetch(QObject *p = 0):
QObject(p),
mgr(new QNetworkAccessManager(this)),
reply(0)
{
QNetworkRequest req(QUrl("https://api.twitter.com/1.1/statuses/update.json"));

// Standard header
req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

// The non-standard header
req.setRawHeader(
QByteArray("Authorization"),
QByteArray( "OAuth "
"oauth_consumer_key=\"ir0j0XHEZAjl5OIMLvRtGDpvz\", "
"oauth_nonce=\"d0d616f462d9d3dd5bb39b23b2ae76ea\", "
"oauth_signature=\"2IAkMds2Pxtp699MReoyupgfZl8%3D\", "
"oauth_signature_method=\"HMAC-SHA1\", "
"oauth_timestamp=\"1441658718\", "
"oauth_token=\"3427524377-klG6CAjAWyWOo9eR7g6WtRm8nGoo9CtQHSX3jpC\", "
"oauth_version=\"1.0\" "
)
);

// The POST payload
QUrlQuery payload;
payload.addQueryItem("status", "ekmek");
QByteArray data = payload.query(QUrl::FullyEncoded).toLocal8Bit();

reply = mgr->post(req, data);
connect(reply, SIGNAL(finished()), SLOT(finished()));
}
private slots:
void error(QNetworkReply::NetworkError code) {
qDebug() << Q_FUNC_INFO << code;
}

void sslErrors(const QList<QSslError> & errors) {
qDebug() << Q_FUNC_INFO << errors;
}

void finished() {
qDebug() << Q_FUNC_INFO << reply->readAll();
reply->deleteLater();
qApp->exit();
}

private:
QNetworkAccessManager *mgr;
QNetworkReply *reply;
};

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

// Something like this if you need it
#if 0
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
QList<QSslCertificate> certs = config.caCertificates();
certs << QSslCertificate::fromPath("cacert.pem", QSsl::Pem);
config.setCaCertificates(certs);
QSslConfiguration::setDefaultConfiguration(config) ;
#endif

Fetch f;
return a.exec();
}

#include "main.moc"

Connects to Twiteer but fails:


void Fetch::finished() "{"errors":[{"code":32,"message":"Could not authenticate you."}]}"

so it clearly connected to Twitter without SSL errors.