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. Google OAuth with QtNetworkAuth read Reply
Forum Updated to NodeBB v4.3 + New Features

Google OAuth with QtNetworkAuth read Reply

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 4.4k 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.
  • N Offline
    N Offline
    Navira
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      3
      • N Offline
        N Offline
        Navira
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • N Offline
          N Offline
          numaelisis
          wrote on last edited by numaelisis
          #4

          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

          veenusavV 1 Reply Last reply
          0
          • N numaelisis

            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

            veenusavV Offline
            veenusavV Offline
            veenusav
            wrote on last edited by
            #5

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

            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