Instantly share code, notes, and snippets.

Embed
What would you like to do?
QString login = "", // Phone / Email
password = "", // password
clientId = "";
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
// чтобы не разводить лишнего кода, будем херачить синхронно
QEventLoop loop;
connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
// Запрашиваем токен с доступом к сообщением
QNetworkReply * reply = manager->get(QNetworkRequest(QUrl("https://oauth.vk.com/authorize?client_id=" + clientId + "&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=messages&response_type=token&v=5.37")));
loop.exec();
// Распарсиваем форму авторизации
QString str( reply->readAll() );
QRegularExpression rx("<input type=\"hidden\" name=\"([a-zA-Z0-9_]+)\" value=\"([a-zA-Z0-9_/:\\.]+)\"", QRegularExpression::MultilineOption);
QRegularExpressionMatchIterator i = rx.globalMatch(str);
QMap<QString, QString> list;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
list[match.captured(1)] = match.captured(2);
}
QString params;
// Добавляем к параметрам логин и пароль
list["email"] = login;
list["pass"] = password;
// Собственно собираем строку с параметрами
for (auto param : list.toStdMap())
params += param.first + "=" + param.second + "&";
// Едем делать авторизацию
reply = manager->get(QNetworkRequest(QUrl("https://login.vk.com/?act=login&soft=1&utf8=1&" + params)));
loop.exec();
// Получаем редирект с успешной авторизацией
reply = manager->get(QNetworkRequest(QUrl(reply->header(QNetworkRequest::LocationHeader).toString())));
loop.exec();
// Получаем редирект на токен, наш милый и любимый
reply = manager->get(QNetworkRequest(QUrl(reply->header(QNetworkRequest::LocationHeader).toString())));
loop.exec();
str = reply->header(QNetworkRequest::LocationHeader).toString();
if (str.indexOf("access_token") != -1)
m_accessToken = str.split("access_token=")[1].split("&")[0];
else
qDebug() << "Faild!";
// Читаем наши сообщения
reply = manager->get(QNetworkRequest(QUrl("https://api.vk.com/method/messages.get?out=0&access_token="+ m_accessToken)));
loop.exec();
qDebug() << reply->readAll();
@PanchukDima

This comment has been minimized.

PanchukDima commented Sep 2, 2015

сейчас работает?

@avengerweb

This comment has been minimized.

Owner

avengerweb commented Nov 25, 2015

Да

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment