-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth.cpp
143 lines (131 loc) · 4.77 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
134
135
136
137
138
139
140
141
142
143
#include "oauth.h"
OAuth* OAuth::m_instance = NULL;
OAuth::OAuth(QObject *parent) :
QObject(parent),
mOauthManager(0),
mOauthRequest(0)
{
}
void OAuth::init( void )
{
deinit();
mOauthRequest = new KQOAuthRequest;
mOauthManager = new KQOAuthManager(this);
mOauthRequest->setEnableDebugOutput(true);
connect(mOauthManager, SIGNAL(receivedToken(QString,QString)),
this, SLOT(onReceivedToken(QString, QString)));
connect(mOauthManager, SIGNAL(temporaryTokenReceived(QString,QString)),
this, SLOT(onTemporaryTokenReceived(QString, QString)));
connect(mOauthManager, SIGNAL(authorizationReceived(QString,QString)),
this, SLOT( onAuthorizationReceived(QString, QString)));
connect(mOauthManager, SIGNAL(accessTokenReceived(QString,QString)),
this, SLOT(onAccessTokenReceived(QString,QString)));
connect(mOauthManager, SIGNAL(requestReady(QByteArray)),
this, SLOT(onRequestReady(QByteArray)));
connect(mOauthManager, SIGNAL(authorizedRequestDone()),
this, SIGNAL(requestDone() ));
}
void OAuth::deinit()
{
if(mOauthRequest)
{
delete mOauthRequest;
mOauthRequest = 0;
}
if(mOauthManager)
{
disconnect(mOauthManager, SIGNAL(receivedToken(QString,QString)),
this, SLOT(onReceivedToken(QString, QString)));
disconnect(mOauthManager, SIGNAL(temporaryTokenReceived(QString,QString)),
this, SLOT(onTemporaryTokenReceived(QString, QString)));
disconnect(mOauthManager, SIGNAL(authorizationReceived(QString,QString)),
this, SLOT( onAuthorizationReceived(QString, QString)));
disconnect(mOauthManager, SIGNAL(accessTokenReceived(QString,QString)),
this, SLOT(onAccessTokenReceived(QString,QString)));
disconnect(mOauthManager, SIGNAL(requestReady(QByteArray)),
this, SLOT(onRequestReady(QByteArray)));
delete mOauthManager;
mOauthManager = 0;
}
}
OAuth::~OAuth()
{
deinit();
}
OAuth* OAuth::instance(){
if(!m_instance){
m_instance = new OAuth();
}
return m_instance;
}
void OAuth::getAccess()
{
init();
QString url = QString("https://www.evernote.com/oauth");
mOauthRequest->initRequest(KQOAuthRequest::TemporaryCredentials, QUrl(url));
QString consumer_key = "locusf";
QString consumer_secret = "011e5c43cc39448c";
mOauthRequest->setConsumerKey(consumer_key);
mOauthRequest->setConsumerSecretKey(consumer_secret);
mOauthManager->setHandleUserAuthorization(true);
if( ! mOauthRequest->isValid() )
{
qDebug() << "Invalid request";
}
mOauthManager->executeRequest(mOauthRequest);
}
void OAuth::onTemporaryTokenReceived(QString token, QString tokenSecret)
{
qDebug() << "Temporary token received: " << token << tokenSecret;
QString url = QString("https://www.evernote.com/OAuth.action");
QUrl userAuthURL(url);
if( mOauthManager->lastError() == KQOAuthManager::NoError)
{
qDebug() << "Asking for user's permission to access protected resources. Opening URL: " << userAuthURL;
browserAuth(mOauthManager->getUserAuthorization(userAuthURL).toString());
}
else
{
qDebug() << "onTemporaryTokenReceived :: OAuth with Evernote failed!";
}
}
void OAuth::onAuthorizationReceived(QString token, QString verifier)
{
qDebug() << "User authorization received: " << token << verifier;
QString url = QString("https://www.evernote.com/oauth");
mOauthManager->getUserAccessTokens(QUrl(url));
if( mOauthManager->lastError() != KQOAuthManager::NoError)
{
qDebug() << "onAuthorizationReceived :: OAuth with Evernote failed!";
}
}
void OAuth::onAccessTokenReceived(QString token, QString store_url)
{
qDebug() << "Access token received: " << token << store_url;
Settings::instance()->setAuthToken(token);
qDebug() << "Access tokens now stored!";
}
void OAuth::onRequestReady(QByteArray response)
{
qDebug() << "Response from the service: " << response;
QString strresp = response;
qDebug() << "Clarified response " << strresp;
if (strresp.lastIndexOf("edam_noteStoreUrl") != -1) {
QStringList tokens = strresp.split("&");
QString oauth_token = QUrl::fromPercentEncoding(tokens.at(0).split("=").at(1).toLatin1());
qDebug() << "Auth token got! " << oauth_token;
Settings::instance()->setAuthToken(oauth_token);
User u;
u.shardId = QUrl::fromPercentEncoding(tokens.at(2).split("=").at(1).toLocal8Bit()).toStdString();
Settings::instance()->setUser(u);
requestDone();
}
if( response == "" )
{
qDebug() << "onRequestReady :: Failed to authorize, empty response from evernote";
}
}
void OAuth::onReceivedToken( QString oauth_token, QString oauth_token_secret )
{
qDebug() << "onReceivedToken: " << oauth_token << oauth_token_secret;
}