Google OAuth with QtNetworkAuth read Reply



  • Hi, everyone. I trying to add OAuth to my desktop app. Found this example http://blog.qt.io/blog/2017/01/25/connecting-qt-application-google-services-using-oauth-2-0/
    The browser opens, I confirm the form, then browser says me: Callback received. Feel free to close this page.
    But I can not read a reply.
    Code

    class googleOAuth: public QObject
    {
        Q_OBJECT
    public slots:
        void googleReply(QNetworkReply *reply)
        {
            qDebug() << "Success";// <<QString::fromUtf8(reply->readAll());
            reply->deleteLater();
        }
    };
    
    
    auto google = new QOAuth2AuthorizationCodeFlow;
    google->setScope("email");
    connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
           &QDesktopServices::openUrl);
    QJsonDocument document = QJsonDocument::fromJson(paramjson.toUtf8());
    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);
    googleOAuth authreply;
    connect(replyHandler, SIGNAL(QNetworkReply::finished), &authreply, SLOT(googleReply(QNetworkReply*)));
    
    google->setReplyHandler(replyHandler);
    
    google->grant();
    auto reply = google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
    
    

    Output

    QML debugging is enabled. Only use this in a safe environment.
    QObject::connect: Parentheses expected, signal QOAuthHttpServerReplyHandler::QNetworkReply::finished in ..\DesktopApp\widget.cpp:36
    QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl: https://accounts.google.com/o/oauth2/auth?client_id=432................
    QOAuthOobReplyHandler::networkReplyFinished: Protocol "https" is unknown
    QOAuth2AuthorizationCodeFlow: Unexpected call
    

    Can somebody help me?



  • connect(replyHandler, SIGNAL(QNetworkReply::finished), &authreply, SLOT(googleReply(QNetworkReply*)));

    this is invalid connect. it should be:
    connect(replyHandler, &QNetworkReply::finished, authreply,[authreply,replyHandler]()->void{authreply->googleReply(replyHandler);});

    See https://wiki.qt.io/New_Signal_Slot_Syntax



  • I could not get a signal.
    I'm trying the other one
    QOAuth2AuthorizationCodeFlow :: grant
    It does not work. Every time I see
    Callback received. Feel free to close this page.
    In the URL received code but I can not read it.
    Log

    QOAuthOobReplyHandler::networkReplyFinished: Protocol "https" is unknown
    QOAuth2AuthorizationCodeFlow: Unexpected call
    

    I compile QT with key -no-openssl Maybe this is the reason?
    Every time I update the page (with "Callback received. Feel free to close this page.") , I see QOAuthOobReplyHandler::networkReplyFinished: Protocol "https" is unknown

    code

    class googleOAuth: public QObject
    {
        Q_OBJECT
    public:
        //googleOAuth(QObject *parent = 0);
    
    
    public slots:
        void googleReply(void)
        {
            qDebug() << "Success";// <<QString::fromUtf8(reply->readAll());
            //reply->deleteLater();
        }
    };
    
    googleOAuth authreply;
    connect(google, &QOAuth2AuthorizationCodeFlow::granted,&authreply,&googleOAuth::googleReply);
    

    If I do not connect signal

    auto google = new QOAuth2AuthorizationCodeFlow;
    google->setScope("email");
    connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
           &QDesktopServices::openUrl);
    QJsonDocument document = QJsonDocument::fromJson(paramjson.toUtf8());
    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);
    google->setReplyHandler(replyHandler);
    googleOAuth authreply;
    //connect(google, &QOAuth2AuthorizationCodeFlow::granted,&authreply,&googleOAuth::googleReply);
    google->grant();
    

    i have this output

    QML debugging is enabled. Only use this in a safe environment.
    QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl: https://accounts.google.com/o/oauth2/auth?client_id=432...........
    QOAuthOobReplyHandler::networkReplyFinished: Protocol "https" is unknown
    QOAuth2AuthorizationCodeFlow: Unexpected call
    I press refresh in bworser
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    QOAuth2AuthorizationCodeFlow: Unexpected call
    

    I really stuck here...



  • Hello,
    I have a similar problem, when I first use the web browser (firefox, config: do not remember history):

    QOAuthOobReplyHandler :: networkReplyFinished: Error transferring https://accounts.google.com/o/oauth2/token - server replied: Bad Request
    QOAuth2AuthorizationCodeFlow: Unexpected call

    But if I repeat google-> grant (); Is solved.

    I would like to know how to catch this error: server replied: Bad Request and Unexpected call

    I already tried with:
    Connect to:
    &QAbstractOAuth2 :: error,
    &QAbstractOAuth :: requestFailed,
    But not cath the error



  • @numaelisis I at your point now. have you got resolution? It seems like QOAuthHttpServerReplyHandler does not have code to handle this situation.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.