#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrlQuery>
#include <QDebug>
#include <QSslConfiguration>
{
Q_OBJECT
public:
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(
"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");
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[])
{
// 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"
#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"
To copy to clipboard, switch view to plain text mode
Connects to Twiteer but fails:
void Fetch::finished() "{"errors":[{"code":32,"message":"Could not authenticate you."}]}"
void Fetch::finished() "{"errors":[{"code":32,"message":"Could not authenticate you."}]}"
To copy to clipboard, switch view to plain text mode
so it clearly connected to Twitter without SSL errors.
Bookmarks