Parsing the access token from the url
[situare] / src / facebookservice / facebookauthentication.cpp
index 12f46e3..398e55d 100644 (file)
@@ -39,6 +39,8 @@
 
 #include "facebookauthentication.h"
 
+const QString REDIRECT_URI = "http://www.facebook.com/connect/login_success.html";
+
 FacebookAuthentication::FacebookAuthentication(QObject *parent)
     : QObject(parent),
     m_freshLogin(false)
@@ -65,6 +67,13 @@ void FacebookAuthentication::clearAccountInformation(bool keepUsername)
     settings.remove(USER_UNSEND_MESSAGE_PUBLISH);
 }
 
+void FacebookAuthentication::loadFinished(bool ok)
+{
+    qWarning() << __PRETTY_FUNCTION__ << ok;
+
+    ///< @todo show browsed window if url != redirect url
+}
+
 const QString FacebookAuthentication::loadUsername()
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -79,6 +88,24 @@ FacebookCredentials FacebookAuthentication::loginCredentials() const
     return m_loginCredentials;
 }
 
+QString FacebookAuthentication::parseAccessToken(const QUrl &url)
+{
+    qWarning() << __PRETTY_FUNCTION__;
+
+    const QString ACCESS_TOKEN_PARAMETER("#access_token=");
+    const QString EXPIRATION_PARAMETER("&expires_in=");
+
+    QString urlString = url.toString();
+
+    int begin = urlString.indexOf(ACCESS_TOKEN_PARAMETER) + ACCESS_TOKEN_PARAMETER.length();
+    int end = urlString.indexOf(EXPIRATION_PARAMETER);
+
+    if ((begin > -1) && (end > begin))
+        return urlString.mid(begin, end - begin);
+    else
+        return QString();
+}
+
 void FacebookAuthentication::saveUsername(const QString &username)
 {
     qDebug() << __PRETTY_FUNCTION__;
@@ -190,3 +217,14 @@ bool FacebookAuthentication::updateCredentials(const QUrl &url)
     }
     return found;
 }
+
+void FacebookAuthentication::urlChanged(const QUrl &url)
+{
+    qWarning() << __PRETTY_FUNCTION__ << url.toString();
+
+    // if login succeeded
+    if (url.toString().startsWith(REDIRECT_URI)) {
+        ///< @todo hide browser dialog
+        qWarning() << __PRETTY_FUNCTION__ << "access_token:" << parseAccessToken(url);
+    }
+}