Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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)
    {
        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.


  • Lifetime Qt Champion

    Hi,

    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:
    "http://127.0.0.1:1234/"


  • 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