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. connection with google on Qt
Forum Update on Monday, May 27th 2025

connection with google on Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 1.5k Views
  • 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.
  • E Offline
    E Offline
    EL-jos
    wrote on last edited by
    #1

    I'm coming to you because I'm facing a problem that I can't solve.

    Here is my problem: for a personal project on Qt, I want to integrate the connection with google (OAuth), here is my code:

    #include "controleur.h"
    #include "ui_controleur.h"
    #include <QOAuth2AuthorizationCodeFlow>
    #include <QDesktopServices>
    #include <QJsonDocument>
    #include <QOAuthHttpServerReplyHandler>
    #include <QJsonDocument>
    #include <QJsonObject>
    #include <QJsonArray>
    #include <QString>
    #include <QFile>
    #include <QDir>
    #include <QUrl>
    #include <QNetworkReply>
    #include <QAbstractOAuth>
    #include <QVariantMap>
    
    Controleur::Controleur(QWidget *parent)
        : QDialog(parent)
        , ui(new Ui::Controleur)
    {
        ui->setupUi(this);
    }
    
    Controleur::~Controleur()
    {
        delete ui;
    }
    
    
    void Controleur::on_pushButton_released()
    {
        auto google = new QOAuth2AuthorizationCodeFlow;
    
        google->setScope("email");
    
        connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,&QDesktopServices::openUrl);
        connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=](){
            //qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
            auto reply = google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
            connect(reply, &QNetworkReply::finished, [reply](){
                qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
                qDebug() << reply->readAll();
            });
        });
    
    
        QByteArray val;
        QFile file;
        file.setFileName(QDir::toNativeSeparators("C:/Users/USER/Downloads/code_secret_client_xxxxxxxxxx.apps.googleusercontent.com.json"));
        if(file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            val = file.readAll();
            file.close();
        }
    
        QJsonDocument document = QJsonDocument::fromJson(val);
        const auto object = document.object();
        const auto settingsObject = object["web"].toObject();
        const QUrl authUri(settingsObject["auth_uri"].toString());
        const auto clientId = settingsObject["client_id"].toString();
        const QUrl tokenUri(settingsObject["token_uri"].toString());
        const auto clientSecret(settingsObject["client_secret"].toString());
        const auto redirectUris = settingsObject["redirect_uris"].toArray();
        const QUrl redirectUri(redirectUris[0].toString()); // Get the first URI
        const auto port = static_cast<quint16>(redirectUri.port()); // Get the port
    
        google->setAuthorizationUrl(authUri);
        google->setClientIdentifier(clientId);
        google->setAccessTokenUrl(tokenUri);
        google->setClientIdentifierSharedKey(clientSecret);
    
        auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
        qDebug() << replyHandler->isListening();
        google->setReplyHandler(replyHandler);
        google->grant();
    
    
    }
    

    when we click on the button everything goes well, the browser also opens without any problem, the user can also select a Google account for the connection to my application, and after the choice of the account by the user here is the message that is displayed in my browser: "Callback received. Feel free to close this page".

    but when I look in the console of my application, here are the messages that are displayed: "true" "qt.networkauth.oauth2: Unexpected call" "qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: "

    which proves that the browser is not able to transmit the data to my application and that's where I'm stuck

    Please tell me where my error is? because I want my application to receive the data from the google account selected by the user in the browser.

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

      Hi,

      Might be a silly question but are you sure the callback URL is correct ?

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

      E 1 Reply Last reply
      1
      • E Offline
        E Offline
        EL-jos
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Might be a silly question but are you sure the callback URL is correct ?

          E Offline
          E Offline
          EL-jos
          wrote on last edited by
          #4

          @SGaist Thank you for your reply,

          here is my redirection URL:
          "http://127.0.0.1:1234/"

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

            @EL-jos said in connection with google on Qt:

            qt.networkauth.oauth2: Unexpected call

            This stack overflow thread might help you.

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

            E 1 Reply Last reply
            0
            • SGaistS SGaist

              @EL-jos said in connection with google on Qt:

              qt.networkauth.oauth2: Unexpected call

              This stack overflow thread might help you.

              E Offline
              E Offline
              EL-jos
              wrote on last edited by
              #6

              @SGaist Hi,
              I'm sorry but I followed the instructions in this link to the letter but still the same problem?

              When copying my code to your site, do you have the same problem?

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

                If you provide a minimal compilable example, it will be easier to test.

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

                E 2 Replies Last reply
                0
                • SGaistS SGaist

                  If you provide a minimal compilable example, it will be easier to test.

                  E Offline
                  E Offline
                  EL-jos
                  wrote on last edited by
                  #8

                  @SGaist the code I posted is my only code, just a small button in a widget (I do it with QtDesign), but the rest of the code is my source code, you can copy/paste it to test

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    If you provide a minimal compilable example, it will be easier to test.

                    E Offline
                    E Offline
                    EL-jos
                    wrote on last edited by
                    #9

                    @SGaist up please

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

                      Your code as is not reusable as you are loading a json file from your hard drive that contains some secrets and some standard information (by the way the toNativeSeparators call is unnecessary as Qt supports forward slashes on all platform and will do the transformation when/if needed).

                      It's also not including the designer .ui file.

                      Hence a minimal compilable example is something that can be directly built minus the secrets information like client_id and other token that shall be changed by the person testing your code. You can put a placeholder text where these secret values shall be changed.

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

                      E 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Your code as is not reusable as you are loading a json file from your hard drive that contains some secrets and some standard information (by the way the toNativeSeparators call is unnecessary as Qt supports forward slashes on all platform and will do the transformation when/if needed).

                        It's also not including the designer .ui file.

                        Hence a minimal compilable example is something that can be directly built minus the secrets information like client_id and other token that shall be changed by the person testing your code. You can put a placeholder text where these secret values shall be changed.

                        E Offline
                        E Offline
                        EL-jos
                        wrote on last edited by SGaist
                        #11

                        @SGaist here is the content of my json file

                        {"web":{"client_id":"<snip>","project_id":"qt-connexion","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"<snip>","redirect_uris":["http://localhost:5476/"],"javascript_origins":["http://localhost","http://localhost:5476"]}}
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Did you saw this updated version of the example code you likely used for your application:

                          https://appfluence.com/productivity/how-to-authenticate-qt-google-sso/

                          ?

                          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