-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.cpp
133 lines (104 loc) · 4.98 KB
/
oauth.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "oauth.h"
Oauth::Oauth(QObject *parent) : QObject(parent) {
oauthRequest = new KQOAuthRequest;
oauthManager = new KQOAuthManager;
oauthRequest->setEnableDebugOutput(true);
}
void Oauth::setSettings(Settings *settings) {
m_sets = settings;
}
void Oauth::startAuthentication(QString username) {
/* query auth Service for available methods */
authService = new SignOn::AuthService();
connect(authService, SIGNAL(methodsAvailable(const QStringList&)),
this, SLOT(identityMethodsAvailable(const QStringList&)));
authService->queryMethods();
SignOn::Identity *m_identity;
OAuth2PluginNS::OAuth1PluginData data;
/* choose oauth2 as method for authentication */
QMap<SignOn::MethodName, SignOn::MechanismsList> methods;
methods.insert("oauth2", QStringList());
m_info = new SignOn::IdentityInfo("Meetumblr", username, methods);
m_identity = SignOn::Identity::newIdentity(*m_info);
SignOn::AuthSession *m_session = m_identity->createSession("oauth2");
data.setRealm("http://api.tumblr.com/v2/");
data.setAuthorizationEndpoint("http://www.tumblr.com/oauth/authorize");
data.setRequestEndpoint("http://www.tumblr.com/oauth/request_token");
data.setTokenEndpoint("http://www.tumblr.com/oauth/access_token");
/* Please read this! ###############
* these are actually our keys ! if you wish to use this code, please register your key / application at: http://www.tumblr.com/oauth/apps
*/
/* ZogG's keys */
//data.setConsumerKey("7zSQ06s60K8PHwXYK1Hw2fTSqgQQkLzPPS14BQQjsXRvQMMWyP");
//data.setConsumerSecret("8rRNTXMWxe6p5AYumqnZYSDDfofBehF1cjuT05w7nqMHpGy0YZ");
/* Niwakame's keys */
data.setConsumerKey(m_sets->getConsumerKey());
data.setConsumerSecret(m_sets->getConsumerSecret());
data.setCallback("http://www.tumblr.com/oauth/access_token");
data.setDisplayCallback("true");
connect(m_session, SIGNAL(response(const SignOn::SessionData &)), this, SLOT(onResponse(const SignOn::SessionData &)));
connect(m_session, SIGNAL(error(const SignOn::Error &)), this, SLOT(onError(const SignOn::Error &)));
m_session->process(data, QString("HMAC-SHA1"));
}
void Oauth::identityMethodsAvailable(const QStringList &methodList) {
for (int i=0; i < methodList.size(); ++i) {
qDebug() << "Method available: " << methodList.at(i).toLocal8Bit().constData();
}
}
void Oauth::onError(const SignOn::Error &error)
{
qDebug() << "Got error:" << error.message();
}
void Oauth::onResponse(const SignOn::SessionData &sessionData)
{
OAuth2PluginNS::OAuth1PluginTokenData response = sessionData.data<OAuth2PluginNS::OAuth1PluginTokenData>();
qDebug() << "Access token: " << response.AccessToken();
qDebug() << "Toke nSecret: " << response.TokenSecret();
m_sets->setAccessToken(response.AccessToken());
m_sets->setAccessTokenSecret(response.TokenSecret());
}
void Oauth::onApiResponse(QByteArray response) {
qDebug() << response;
}
void Oauth::onApiError(QByteArray response) {
}
/* test function to get a profile */
void Oauth::testCall(QString test) {
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/user/info"));
//oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, QUrl("http://api.tumblr.com/v2/blog/niwakame.tumblr.com/post"));
oauthRequest->setConsumerKey(m_sets->getConsumerKey());
oauthRequest->setConsumerSecretKey(m_sets->getConsumerSecret());
oauthRequest->setToken(m_sets->getAccessToken());
oauthRequest->setTokenSecret(m_sets->getAccessTokenSecret());
//oauthRequest->setHttpMethod(KQOAuthRequest::POST);
/*KQOAuthParameters params;
params.insert("type", "text");
params.insert("body", "This is a testpost from Meemblr. Fear us :>");
oauthRequest->setAdditionalParameters(params);*/
connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onApiResponse(QByteArray)));
oauthManager->executeRequest(oauthRequest);
}
void Oauth::callApi(QUrl url, QList< QPair<QString, QString> > params, QString method) {
oauthRequest->initRequest(KQOAuthRequest::AuthorizedRequest, url);
oauthRequest->setConsumerKey(m_sets->getConsumerKey());
oauthRequest->setConsumerSecretKey(m_sets->getConsumerSecret());
oauthRequest->setToken(m_sets->getAccessToken());
oauthRequest->setTokenSecret(m_sets->getAccessTokenSecret());
if (method == "POST") {
oauthRequest->setHttpMethod(KQOAuthRequest::POST);
} else {
oauthRequest->setHttpMethod(KQOAuthRequest::GET);
}
if (params.size() > 0) {
KQOAuthParameters parameters;
QPair<QString, QString> pair;
foreach(pair, params) {
QString key = pair.first;
QString val = pair.second;
parameters.insert(key, val);
}
oauthRequest->setAdditionalParameters(parameters);
}
connect(oauthManager, SIGNAL(requestReady(QByteArray)), this, SLOT(onApiResponse(QByteArray)));
oauthManager->executeRequest(oauthRequest);
}