Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Help with Google Sign-In
Forum Updated to NodeBB v4.3 + New Features

Help with Google Sign-In

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 638 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • franco.amatoF Offline
    franco.amatoF Offline
    franco.amato
    wrote on last edited by
    #1

    I have to implement the Google Sign-In in my desktop application (I am using Qt 6.7.2).
    I added a googleLoginButton button with the following slot:

    void Login::on_googleLoginButton_clicked()
    {
        m_googleOAuth2->grant();
    }
    

    In the ctor of my class I setup the Google Auth

    Login::Login(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Login)
    {
        ui->setupUi(this);
        setupGoogleAuth();
    }
    

    Here the google auth setup

    void Login::setupGoogleAuth()
    {
        m_googleOAuth2 = new QOAuth2AuthorizationCodeFlow(this);
    
        // Set client credentials
        m_googleOAuth2->setClientIdentifier("947359679464-jq8q0nscv9nioa3ig4lobrcsoc2tlpos.apps.googleusercontent.com");
        m_googleOAuth2->setClientIdentifierSharedKey("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");
    
        // Set authorization and token URLs
        m_googleOAuth2->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth"));
        m_googleOAuth2->setAccessTokenUrl(QUrl("https://accounts.google.com/o/auth2/token"));
        
        // Set the scopes
        m_googleOAuth2->setScope("openid email profile");
    
        // Set the reply handler
        m_replyHandler = new QOAuthHttpServerReplyHandler(1337, this);
        m_googleOAuth2->setReplyHandler(m_replyHandler);
    
        // Connect signals and slots
        connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
                &QDesktopServices::openUrl);
        connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::granted, this, &Login::onGoogleLoginSuccess);
        connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::error, this,
                [this](const QString &error, const QString &errorDescription, const QUrl &uri) {
            onGoogleLoginError(error, errorDescription, uri);
        });
    }
    

    In case of success

    void Login::onGoogleLoginSuccess()
    {
        qDebug() << "AMATO: Google login successful";
        QString accessToken = m_googleOAuth2->token();
    
        // Save the access token securely
        saveAccessToken(accessToken);
    
        // Fetch user profile
        fetchGoogleUserProfile();
    
        QJsonObject requestData;
        requestData["access_token"] = accessToken;
    
        auto mgr = new QNetworkAccessManager(this);
        QNetworkReply *reply = BBHelper::HttpRequest(POST,
                                                     mgr,
                                                     "/api/google/login",
                                                     &requestData);
    
        connect(reply, &QNetworkReply::finished, this, [this, reply]() {
            handleBackendResponse(reply);
        });
    }
    

    In case of error:

    void Login::onGoogleLoginError(const QString &error, const QString &errorDescription, const QUrl &uri)
    {
        qWarning() << "AMATO: Google login error:" << error << errorDescription << uri.toString();
        QMessageBox::warning(this, "Login Error", "Failed to log in with Google " + errorDescription);
    }
    

    Unfortunately neither onGoogleLoginSuccess nor onGoogleLoginError is called and I cannot understand why. The cliend_id and client_secret are correct.
    I would like to understand what I am doing wrong

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Don't see where you start the whole thing: https://doc.qt.io/qt-6/qoauth2authorizationcodeflow.html#grant

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • franco.amatoF Offline
        franco.amatoF Offline
        franco.amato
        wrote on last edited by
        #3

        @Christian-Ehrlicher here:
        void Login::on_googleLoginButton_clicked()
        {
        m_googleOAuth2->grant();
        }

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          And do you get there?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • franco.amatoF Offline
            franco.amatoF Offline
            franco.amato
            wrote on last edited by
            #5

            When I press the button, a web page opens so that i can choose the google account but after that nothing happens

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You should connect the error signal to see if something wrong is going on.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • franco.amatoF Offline
                franco.amatoF Offline
                franco.amato
                wrote on last edited by franco.amato
                #7

                @SGaist The connection is already there. Now I could solve some problems but I noticed that I cannot get the token if I set the scope in this way:

                m_oauth->setScope("email");
                or
                m_oauth->setScope("https://www.googleapis.com/auth/userinfo.email");
                

                My only purpose is to get the user email. To get it working I have to set the scope in this way:

                m_oauth->setScope("email https://www.googleapis.com/auth/drive.readonly");
                

                I added to enable the drive api for that. For the first case I enabled People API and nothing. I cannot understand the problem

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Did you set the required scopes in your Google OAuth application ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved