/
VKApi.cpp
160 lines (127 loc) · 4.41 KB
/
VKApi.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <boost/algorithm/string/join.hpp>
#include <boost/bind.hpp>
#include <QUrlQuery>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QJsonObject>
#include <QJsonArray>
#include "VKApi.h"
static const QString kApiBaseUrl = "https://api.vk.com/method/";
static const QString kAuthUrl = "https://oauth.vk.com/authorize";
static const QString kRedirectUrl = "http://oauth.vk.com/blank.html";
namespace poison
{
VKApi::VKApi()
: loginView(0)
, user(new User())
{
}
bool VKApi::isLoggedIn() const {
return accessToken.length();
}
void VKApi::method(const QString &method,
const QMap<QString, QString> params,
std::function< void(const QJsonDocument*, QNetworkReply::NetworkError) > callback
) {
QUrl url( kApiBaseUrl + method );
QUrlQuery q;
if (!params.empty()) {
for (const auto& key : params) {
q.addQueryItem( key, params.value(key) );
}
}
if (isLoggedIn())
q.addQueryItem("access_token", accessToken);
url.setQuery(q);
QNetworkRequest req;
req.setUrl(url);
qDebug() << "Sending request to: " << req.url().toString() << "\n";
auto reply = net.get(req);
connect(reply, &QNetworkReply::finished, [reply, callback]() {
QJsonDocument* json = 0;
QJsonDocument jsonDoc;
QNetworkReply::NetworkError error = reply->error();
qDebug() << "Finished reply: " << reply << "\n";
if (reply->error() != QNetworkReply::NetworkError::NoError) {
qDebug() << "Network error: " << reply->errorString() << "\n";
} else {
QJsonParseError parseErr;
auto data = reply->readAll();
jsonDoc = QJsonDocument::fromJson(data , &parseErr );
qDebug() << "got response <" << reply->url() << ">\n" << data << "\n\n\n";
if (parseErr.error != QJsonParseError::NoError) {
qDebug() << "failed to parse json: " << parseErr.errorString() << "\n";
error = QNetworkReply::NetworkError::UnknownContentError;
} else if (!jsonDoc.object().contains("response")) {
qDebug() << "bad json.\n";
error = QNetworkReply::NetworkError::UnknownContentError;
} else {
jsonDoc = QJsonDocument( jsonDoc.object().value( "response" ).toArray().at(0).toObject() );
json = &jsonDoc;
}
}
if (callback)
callback(json, error);
reply->deleteLater();
});
}
QUrl VKApi::getLoginUrl() const {
QUrl url(kAuthUrl);
QUrlQuery q;
q.addQueryItem("client_id", appId);
q.addQueryItem("scope", scope);
q.addQueryItem("display", "popup");
q.addQueryItem("redirect_uri", kRedirectUrl);
q.addQueryItem("response_type", "token");
url.setQuery(q);
return url;
}
void VKApi::init(const QString& appId, const QString& scope)
{
this->appId = appId;
this->scope = scope;
qDebug() << "VK inited";
}
void VKApi::login() {
if (isLoggedIn() || loginView) {
return;
}
auto webView = new QWebView();
webView->setAttribute(Qt::WA_DeleteOnClose, true);
auto url = getLoginUrl();
webView->load( url );
connect(webView, SIGNAL(urlChanged(QUrl)), this, SLOT(loginUrlChanged(const QUrl&)));
connect(webView, &QWebView::destroyed, [this](){
this->loginView = 0;
});
webView->show();
loginView = webView;
}
void VKApi::loginUrlChanged(const QUrl& url) {
qDebug() << "login url filename: \n" << url.fileName() << "\n";
if (url.fileName() == "blank.html") {
QUrlQuery q(url.fragment());
accessToken = q.queryItemValue("access_token");
qDebug() << "got access token: " << accessToken << "\n";
QMap<QString, QString> params;
method("users.get", params, [this]( const QJsonDocument* json, QNetworkReply::NetworkError) {
loginView->close();
loginView = 0;
if (json) {
auto obj = json->object();
qDebug() << obj.keys().join(",");
user->_name = obj["first_name"].toString() + " " + obj["last_name"].toString();
user->_uid = QString::number( obj["uid"].toInt() );
} else {
this->logout();
this->login();
}
emit loginStatusChanged();
});
loginView->setVisible(false);
}
}
void VKApi::logout() {
accessToken.clear();
}
}