Important: Please read the Qt Code of Conduct -

connection with google on Qt

  • 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)
        delete ui;
    void Controleur::on_pushButton_released()
        auto google = new QOAuth2AuthorizationCodeFlow;
        connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,&QDesktopServices::openUrl);
        connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=](){
            //qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
            auto reply = google->get(QUrl(""));
            connect(reply, &QNetworkReply::finished, [reply](){
                qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
                qDebug() << reply->readAll();
        QByteArray val;
        QFile file;
        if( | QIODevice::Text))
            val = file.readAll();
        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
        auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
        qDebug() << replyHandler->isListening();

    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 - 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.

  • Lifetime Qt Champion


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

  • This post is deleted!

  • @SGaist Thank you for your reply,

    here is my redirection URL:

  • Lifetime Qt Champion

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

    qt.networkauth.oauth2: Unexpected call

    This stack overflow thread might help you.

Log in to reply