File indexing completed on 2025-01-19 03:55:39
0001 #include <QDebug> 0002 #include <QJsonDocument> 0003 #include <QJsonObject> 0004 #include <QMap> 0005 #include <QNetworkReply> 0006 #include <QString> 0007 #include <QStringList> 0008 #include <QUrl> 0009 #if QT_VERSION >= 0x050000 0010 #include <QUrlQuery> 0011 #endif 0012 0013 #include "o2facebook.h" 0014 #include "o0globals.h" 0015 0016 static const char *FbEndpoint = "https://graph.facebook.com/oauth/authorize?display=touch"; 0017 static const char *FbTokenUrl = "https://graph.facebook.com/oauth/access_token"; 0018 static const char *FbExpiresKey = "expires_in"; 0019 0020 O2Facebook::O2Facebook(QObject *parent): O2(parent) { 0021 setRequestUrl(FbEndpoint); 0022 setTokenUrl(FbTokenUrl); 0023 } 0024 0025 void O2Facebook::onVerificationReceived(const QMap<QString, QString> response) { 0026 qDebug() << "O2Facebook::onVerificationReceived: Emitting closeBrowser()"; 0027 Q_EMIT closeBrowser(); 0028 0029 if (response.contains("error")) { 0030 qWarning() << "O2Facebook::onVerificationReceived: Verification failed"; 0031 foreach (QString key, response.keys()) { 0032 qWarning() << "O2Facebook::onVerificationReceived:" << key << response.value(key); 0033 } 0034 Q_EMIT linkingFailed(); 0035 return; 0036 } 0037 0038 // Save access code 0039 setCode(response.value(O2_OAUTH2_GRANT_TYPE_CODE)); 0040 0041 // Exchange access code for access/refresh tokens 0042 QUrl url(tokenUrl_); 0043 #if QT_VERSION < 0x050000 0044 url.addQueryItem(O2_OAUTH2_CLIENT_ID, clientId_); 0045 url.addQueryItem(O2_OAUTH2_CLIENT_SECRET, clientSecret_); 0046 url.addQueryItem(O2_OAUTH2_SCOPE, scope_); 0047 url.addQueryItem(O2_OAUTH2_GRANT_TYPE_CODE, code()); 0048 url.addQueryItem(O2_OAUTH2_REDIRECT_URI, redirectUri_); 0049 #else 0050 QUrlQuery query(url); 0051 query.addQueryItem(O2_OAUTH2_CLIENT_ID, clientId_); 0052 query.addQueryItem(O2_OAUTH2_CLIENT_SECRET, clientSecret_); 0053 query.addQueryItem(O2_OAUTH2_SCOPE, scope_); 0054 query.addQueryItem(O2_OAUTH2_GRANT_TYPE_CODE, code()); 0055 query.addQueryItem(O2_OAUTH2_REDIRECT_URI, redirectUri_); 0056 url.setQuery(query); 0057 #endif 0058 0059 QNetworkRequest tokenRequest(url); 0060 QNetworkReply *tokenReply = manager_->get(tokenRequest); 0061 timedReplies_.add(tokenReply); 0062 connect(tokenReply, SIGNAL(finished()), this, SLOT(onTokenReplyFinished()), Qt::QueuedConnection); 0063 #if QT_VERSION < 0x051500 0064 connect(tokenReply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection); 0065 #else 0066 connect(tokenReply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(onTokenReplyError(QNetworkReply::NetworkError)), Qt::QueuedConnection); 0067 #endif 0068 } 0069 0070 void O2Facebook::onTokenReplyFinished() { 0071 qDebug() << "O2Facebook::onTokenReplyFinished"; 0072 0073 QNetworkReply *tokenReply = qobject_cast<QNetworkReply *>(sender()); 0074 if (tokenReply->error() == QNetworkReply::NoError) { 0075 // Process reply 0076 QByteArray replyData = tokenReply->readAll(); 0077 QJsonDocument doc = QJsonDocument::fromJson(replyData); 0078 const QJsonObject rootObject = doc.object(); 0079 0080 QVariantMap reply; 0081 for (const QString &key : rootObject.keys()) { 0082 reply.insert(key, rootObject[key].toVariant()); 0083 } 0084 0085 // Interpret reply 0086 setToken(reply.value(O2_OAUTH2_ACCESS_TOKEN, QString()).toString()); 0087 setExpires(reply.value(FbExpiresKey).toInt()); 0088 setRefreshToken(reply.value(O2_OAUTH2_REFRESH_TOKEN, QString()).toString()); 0089 setExtraTokens(reply); 0090 timedReplies_.remove(tokenReply); 0091 setLinked(true); 0092 Q_EMIT linkingSucceeded(); 0093 } else { 0094 qWarning() << "O2Facebook::onTokenReplyFinished:" << tokenReply->errorString(); 0095 } 0096 } 0097 0098 #include "moc_o2facebook.cpp"