| 1 |
/** |
| 2 |
* TwitterCLI - This file is a part of the kQOAuth library. |
| 3 |
* |
| 4 |
* Author: Johan Paul (johan.paul@d-pointer.com) |
| 5 |
* http://www.d-pointer.com |
| 6 |
* |
| 7 |
* KQOAuth is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Lesser General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* KQOAuth is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Lesser General Public License |
| 18 |
* along with KQOAuth. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
#include <QCoreApplication> |
| 21 |
#include <QStringList> |
| 22 |
#include <QtDebug> |
| 23 |
|
| 24 |
#include <QtKOAuth> |
| 25 |
|
| 26 |
#include "twittercli.h" |
| 27 |
|
| 28 |
TwitterCLI::TwitterCLI() { |
| 29 |
oauthRequest = new KQOAuthRequest; |
| 30 |
oauthManager = new KQOAuthManager(this); |
| 31 |
|
| 32 |
oauthRequest->setEnableDebugOutput(true); |
| 33 |
} |
| 34 |
|
| 35 |
TwitterCLI::~TwitterCLI() { |
| 36 |
delete oauthRequest; |
| 37 |
delete oauthManager; |
| 38 |
} |
| 39 |
|
| 40 |
void TwitterCLI::getAccess() { |
| 41 |
connect(oauthManager, SIGNAL(temporaryTokenReceived(QString,QString)), |
| 42 |
this, SLOT(onTemporaryTokenReceived(QString, QString))); |
| 43 |
|
| 44 |
connect(oauthManager, SIGNAL(authorizationReceived(QString,QString)), |
| 45 |
this, SLOT( onAuthorizationReceived(QString, QString))); |
| 46 |
|
| 47 |
connect(oauthManager, SIGNAL(accessTokenReceived(QString,QString)), |
| 48 |
this, SLOT(onAccessTokenReceived(QString,QString))); |
| 49 |
|
| 50 |
connect(oauthManager, SIGNAL(requestReady(QByteArray)), |
| 51 |
this, SLOT(onRequestReady(QByteArray))); |
| 52 |
|
| 53 |
oauthRequest->initRequest(KQOAuthRequest::TemporaryCredentials, QUrl("https://api.twitter.com/oauth/request_token")); |
| 54 |
oauthRequest->setConsumerKey("9PqhX2sX7DlmjNJ5j2Q"); |
| 55 |
oauthRequest->setConsumerSecretKey("1NYYhpIw1fXItywS9Bw6gGRmkRyF9zB54UXkTGcI8"); |
| 56 |
|
| 57 |
oauthManager->setHandleUserAuthorization(true); |
| 58 |
|
| 59 |
oauthManager->executeRequest(oauthRequest); |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
void TwitterCLI::onTemporaryTokenReceived(QString token, QString tokenSecret) |
| 64 |
{ |
| 65 |
qDebug() << "Temporary token received: " << token << tokenSecret; |
| 66 |
|
| 67 |
QUrl userAuthURL("https://api.twitter.com/oauth/authorize"); |
| 68 |
|
| 69 |
if( oauthManager->lastError() == KQOAuthManager::NoError) { |
| 70 |
qDebug() << "Asking for user's permission to access protected resources. Opening URL: " << userAuthURL; |
| 71 |
oauthManager->getUserAuthorization(userAuthURL); |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
void TwitterCLI::onAuthorizationReceived(QString token, QString verifier) { |
| 77 |
qDebug() << "User authorization received: " << token << verifier; |
| 78 |
|
| 79 |
oauthManager->getUserAccessTokens(QUrl("https://api.twitter.com/oauth/access_token")); |
| 80 |
if( oauthManager->lastError() != KQOAuthManager::NoError) { |
| 81 |
QCoreApplication::exit(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void TwitterCLI::onAccessTokenReceived(QString token, QString tokenSecret) { |
| 86 |
qDebug() << "Access token received: " << token << tokenSecret; |
| 87 |
|
| 88 |
oauthSettings.setValue("oauth_token", token); |
| 89 |
oauthSettings.setValue("oauth_token_secret", tokenSecret); |
| 90 |
|
| 91 |
qDebug() << "Access tokens now stored. You are ready to send Tweets from user's account!"; |
| 92 |
|
| 93 |
QCoreApplication::exit(); |
| 94 |
} |
| 95 |
|
| 96 |
void TwitterCLI::onAuthorizedRequestDone() { |
| 97 |
qDebug() << "Request sent to Twitter!"; |
| 98 |
QCoreApplication::exit(); |
| 99 |
} |
| 100 |
|
| 101 |
void TwitterCLI::onRequestReady(QByteArray response) { |
| 102 |
qDebug() << "Response from the service: " << response; |
| 103 |
} |
| 104 |
|
| 105 |
void TwitterCLI::xauth() { |
| 106 |
connect(oauthManager, SIGNAL(accessTokenReceived(QString,QString)), |
| 107 |
this, SLOT(onAccessTokenReceived(QString,QString))); |
| 108 |
|
| 109 |
KQOAuthRequest_XAuth *oauthRequest = new KQOAuthRequest_XAuth(this); |
| 110 |
oauthRequest->initRequest(KQOAuthRequest::AccessToken, QUrl("https://api.twitter.com/oauth/access_token")); |
| 111 |
oauthRequest->setConsumerKey("9PqhX2sX7DlmjNJ5j2Q"); |
| 112 |
oauthRequest->setConsumerSecretKey("1NYYhpIw1fXItywS9Bw6gGRmkRyF9zB54UXkTGcI8"); |
| 113 |
oauthRequest->setXAuthLogin(/* Your username*/ /*,*/ /* Your password */); |
| 114 |
|
| 115 |
oauthManager->executeRequest(oauthRequest); |
| 116 |
} |
| 117 |
|
| 118 |
void TwitterCLI::sendTweet(QString tweet) { |
| 119 |
|
| 120 |
if( oauthSettings.value("oauth_token").toString().isEmpty() || |
| 121 |
oauthSettings.value("oauth_token_secret").toString().isEmpty()) { |
| 122 |
qDebug() << "No access tokens. Aborting."; |
| 123 |
|
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.twitter.com/1/statuses/update.xml")); |
| 128 |
oauthRequest->setConsumerKey("9PqhX2sX7DlmjNJ5j2Q"); |
| 129 |
oauthRequest->setConsumerSecretKey("1NYYhpIw1fXItywS9Bw6gGRmkRyF9zB54UXkTGcI8"); |
| 130 |
oauthRequest->setToken(oauthSettings.value("oauth_token").toString()); |
| 131 |
oauthRequest->setTokenSecret(oauthSettings.value("oauth_token_secret").toString()); |
| 132 |
|
| 133 |
KQOAuthParameters params; |
| 134 |
params.insert("status", tweet); |
| 135 |
oauthRequest->setAdditionalParameters(params); |
| 136 |
|
| 137 |
oauthManager->executeRequest(oauthRequest); |
| 138 |
|
| 139 |
connect(oauthManager, SIGNAL(requestReady(QByteArray)), |
| 140 |
this, SLOT(onRequestReady(QByteArray))); |
| 141 |
connect(oauthManager, SIGNAL(authorizedRequestDone()), |
| 142 |
this, SLOT(onAuthorizedRequestDone())); |
| 143 |
} |
| 144 |
|
| 145 |
void TwitterCLI::showHelp() { |
| 146 |
QTextStream qout(stdout); |
| 147 |
qout << "TwitterCLI, version 0.95. Author: Johan Paul <johan.paul@d-pointer.com>\n" |
| 148 |
<< "\n" |
| 149 |
<< "Usage: twittercli -[at] <tweet>\n" |
| 150 |
<< " -a Request for access tokens.\n" |
| 151 |
<< " -x Use Twitter xAuth to retrieve access tokens.\n" |
| 152 |
<< " -t '<tweet>' Send <tweet> to Twitter after retrieving access tokens\n" |
| 153 |
<< "\n"; |
| 154 |
} |
| 155 |
|
| 156 |
int main(int argc, char *argv[]) |
| 157 |
{ |
| 158 |
QCoreApplication app(argc, argv); |
| 159 |
QCoreApplication::setOrganizationName("kQOAuth"); |
| 160 |
QCoreApplication::setApplicationName("TwitterCLI"); |
| 161 |
|
| 162 |
QStringList args = QCoreApplication::arguments(); |
| 163 |
|
| 164 |
TwitterCLI tAuth; |
| 165 |
if(args.contains("-t")) { |
| 166 |
if(args.last() != "-t") { |
| 167 |
tAuth.sendTweet(args.last()); |
| 168 |
} |
| 169 |
} else if( args.contains("-a")){ |
| 170 |
tAuth.getAccess(); |
| 171 |
} else if (args.contains("-x")) { |
| 172 |
tAuth.xauth(); |
| 173 |
} else { |
| 174 |
tAuth.showHelp(); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
return app.exec(); |
| 179 |
|
| 180 |
} |