File indexing completed on 2025-01-19 03:55:36

0001 #include <QNetworkAccessManager>
0002 #include <QNetworkRequest>
0003 #include <QNetworkReply>
0004 #include <QDesktopServices>
0005 #include <QMetaEnum>
0006 #include <QDebug>
0007 
0008 #include "fbdemo.h"
0009 #include "o0globals.h"
0010 #include "o0settingsstore.h"
0011 
0012 const char FB_APP_KEY[] = "227896037359072";
0013 const char FB_APP_SECRET[] = "3d35b063872579cf7213e09e76b65ceb";
0014 
0015 const char FB_REQUEST_URL[] = "https://www.facebook.com/dialog/oauth";
0016 const char FB_DEBUG_TOKEN[] = "https://graph.facebook.com/me?fields=id&access_token=%1";
0017 
0018 const int localPort = 8888;
0019 
0020 #define QENUM_NAME(o,e,v) (o::staticMetaObject.enumerator(o::staticMetaObject.indexOfEnumerator(#e)).valueToKey((v)))
0021 #define GRANTFLOW_STR(v) QString(QENUM_NAME(O2, GrantFlow, v))
0022 
0023 FBDemo::FBDemo(QObject *parent) :
0024     QObject(parent) {
0025     o2Facebook_ = new O2Facebook(this);
0026 
0027     o2Facebook_->setClientId(FB_APP_KEY);
0028     o2Facebook_->setClientSecret(FB_APP_SECRET);
0029     o2Facebook_->setLocalPort(localPort);
0030     o2Facebook_->setRequestUrl(FB_REQUEST_URL);  // Use the desktop login UI
0031 
0032     // Create a store object for writing the received tokens
0033     O0SettingsStore *store = new O0SettingsStore(O2_ENCRYPTION_KEY);
0034     store->setGroupKey("facebook");
0035     o2Facebook_->setStore(store);
0036 
0037     connect(o2Facebook_, SIGNAL(linkedChanged()), this, SLOT(onLinkedChanged()));
0038     connect(o2Facebook_, SIGNAL(linkingFailed()), this, SIGNAL(linkingFailed()));
0039     connect(o2Facebook_, SIGNAL(linkingSucceeded()), this, SLOT(onLinkingSucceeded()));
0040     connect(o2Facebook_, SIGNAL(openBrowser(QUrl)), this, SLOT(onOpenBrowser(QUrl)));
0041     connect(o2Facebook_, SIGNAL(closeBrowser()), this, SLOT(onCloseBrowser()));
0042 }
0043 
0044 void FBDemo::doOAuth(O2::GrantFlow grantFlowType) {
0045     qDebug() << "Starting OAuth 2 with grant flow type" << GRANTFLOW_STR(grantFlowType) << "...";
0046     o2Facebook_->setGrantFlow(grantFlowType);
0047     o2Facebook_->unlink();
0048     o2Facebook_->link();
0049 }
0050 
0051 void FBDemo::validateToken() {
0052     if (!o2Facebook_->linked()) {
0053         qWarning() << "ERROR: Application is not linked!";
0054         emit linkingFailed();
0055         return;
0056     }
0057 
0058     QString accessToken = o2Facebook_->token();
0059     QString debugUrlStr = QString(FB_DEBUG_TOKEN).arg(accessToken);
0060     QNetworkRequest request = QNetworkRequest(QUrl(debugUrlStr));
0061     QNetworkAccessManager *mgr = new QNetworkAccessManager(this);
0062     QNetworkReply *reply = mgr->get(request);
0063     connect(reply, SIGNAL(finished()), this, SLOT(onFinished()));
0064     qDebug() << "Validating user token. Please wait...";
0065 }
0066 
0067 void FBDemo::onOpenBrowser(const QUrl &url) {
0068     QDesktopServices::openUrl(url);
0069 }
0070 
0071 void FBDemo::onCloseBrowser() {
0072 }
0073 
0074 void FBDemo::onLinkedChanged() {
0075     qDebug() << "Link changed!";
0076 }
0077 
0078 void FBDemo::onLinkingSucceeded() {
0079     O2Facebook *o1t = qobject_cast<O2Facebook *>(sender());
0080     if (!o1t->linked()) {
0081         return;
0082     }
0083     QVariantMap extraTokens = o1t->extraTokens();
0084     if (!extraTokens.isEmpty()) {
0085         emit extraTokensReady(extraTokens);
0086         qDebug() << "Extra tokens in response:";
0087         foreach (QString key, extraTokens.keys()) {
0088             qDebug() << "\t" << key << ":" << (extraTokens.value(key).toString().left(3) + "...");
0089         }
0090     }
0091     emit linkingSucceeded();
0092 }
0093 
0094 void FBDemo::onFinished() {
0095     QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
0096     if (!reply) {
0097         qWarning() << "NULL reply!";
0098         emit linkingFailed();
0099         return;
0100     }
0101 
0102     reply->deleteLater();
0103     if (reply->error() != QNetworkReply::NoError) {
0104         qWarning() << "Reply error:" << reply->error();
0105         qWarning() << "Reason:" << reply->errorString();
0106         emit linkingFailed();
0107         return;
0108     }
0109 
0110     QByteArray replyData = reply->readAll();
0111     bool valid = !replyData.contains("error");
0112     if (valid) {
0113         qDebug() << "Token is valid";
0114         emit linkingSucceeded();
0115     } else {
0116         qDebug() << "Token is invalid";
0117         emit linkingFailed();
0118     }
0119 }
0120 
0121 #include "moc_fbdemo.cpp"