File indexing completed on 2025-01-19 03:55:40

0001 #include <QDateTime>
0002 #include <QDebug>
0003 
0004 #include "oxtwitter.h"
0005 #include "o0globals.h"
0006 
0007 const char XAUTH_USERNAME[] = "x_auth_username";
0008 const char XAUTH_PASSWORD[] = "x_auth_password";
0009 const char XAUTH_MODE[] = "x_auth_mode";
0010 const char XAUTH_MODE_VALUE[] = "client_auth";
0011 
0012 OXTwitter::OXTwitter(QObject *parent): O1Twitter(parent) {
0013 }
0014 
0015 QString OXTwitter::username() {
0016     return username_;
0017 }
0018 
0019 void OXTwitter::setUsername(const QString &username) {
0020     username_ = username;
0021     Q_EMIT usernameChanged();
0022 }
0023 
0024 QString OXTwitter::password() {
0025     return password_;
0026 }
0027 
0028 void OXTwitter::setPassword(const QString &password) {
0029     password_ = password;
0030     Q_EMIT passwordChanged();
0031 }
0032 
0033 void OXTwitter::link() {
0034     qDebug() << "OXTwitter::link";
0035     if (linked()) {
0036         qDebug() << "Linked already";
0037         return;
0038     }
0039 
0040     if (username_.isEmpty() || password_.isEmpty()) {
0041         qWarning() << "Error: XAuth parameters not set. Aborting!";
0042         return;
0043     }
0044 
0045     // prepare XAuth parameters
0046     xAuthParams_.append(O0RequestParameter(QByteArray(XAUTH_USERNAME), username_.toLatin1()));
0047     xAuthParams_.append(O0RequestParameter(QByteArray(XAUTH_PASSWORD), password_.toLatin1()));
0048     xAuthParams_.append(O0RequestParameter(QByteArray(XAUTH_MODE), QByteArray(XAUTH_MODE_VALUE)));
0049 
0050     QList<O0RequestParameter> oauthParams;
0051     oauthParams.append(O0RequestParameter(O2_OAUTH_SIGNATURE_METHOD, O2_SIGNATURE_TYPE_HMAC_SHA1));
0052     oauthParams.append(O0RequestParameter(O2_OAUTH_CONSUMER_KEY, clientId().toLatin1()));
0053     oauthParams.append(O0RequestParameter(O2_OAUTH_VERSION, "1.0"));
0054 #if QT_VERSION >= 0x050800
0055     oauthParams.append(O0RequestParameter(O2_OAUTH_TIMESTAMP, QString::number(QDateTime::currentSecsSinceEpoch()).toLatin1()));
0056 #else
0057     oauthParams.append(O0RequestParameter(O2_OAUTH_TIMESTAMP, QString::number(QDateTime::currentDateTimeUtc().toTime_t()).toLatin1()));
0058 #endif
0059     oauthParams.append(O0RequestParameter(O2_OAUTH_NONCE, nonce()));
0060     oauthParams.append(O0RequestParameter(O2_OAUTH_TOKEN, QByteArray("")));
0061     oauthParams.append(O0RequestParameter(O2_OAUTH_VERFIER, QByteArray("")));
0062 
0063     QByteArray signature = sign(oauthParams, xAuthParams_, accessTokenUrl(), QNetworkAccessManager::PostOperation, clientSecret(), "");
0064     oauthParams.append(O0RequestParameter(O2_OAUTH_SIGNATURE, signature));
0065 
0066     // Post request
0067     QNetworkRequest request(accessTokenUrl());
0068     decorateRequest(request, oauthParams);
0069     request.setHeader(QNetworkRequest::ContentTypeHeader, O2_MIME_TYPE_XFORM);
0070     QNetworkReply *reply = manager_->post(request, createQueryParameters(xAuthParams_));
0071 #if QT_VERSION < 0x051500
0072     connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onTokenExchangeError(QNetworkReply::NetworkError)));
0073 #else
0074     connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(onTokenExchangeError(QNetworkReply::NetworkError)));
0075 #endif
0076     connect(reply, SIGNAL(finished()), this, SLOT(onTokenExchangeFinished()));
0077 }
0078 
0079 #include "moc_oxtwitter.cpp"