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. QAbstractOAuth2 missing access_type and approval_prompt?
Forum Updated to NodeBB v4.3 + New Features

QAbstractOAuth2 missing access_type and approval_prompt?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 5 Posters 962 Views 2 Watching
  • 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.
  • T Offline
    T Offline
    TenG
    wrote on last edited by
    #1

    The (horribly convoluted) code I (a novice) have inherited uses the QAbstractOAuth2 class to implement OAuth2 access to Google drive.

    The app only succeeds to gain a access token for Google Drive if the app's access is revoked from the Google Drive control panel. But subsequent requests fail with the obscure 'bad request' error.

    I set up a test using curl and found that adding the parameters access_type=offline&approval_prompt=force to the URL results in the access working.

    So now I want to try and add this in my Qt code.

    _oAuth2 = new QOAuth2AuthorizationCodeFlow( this);
        _oAuth2->setScope( "https://www.googleapis.com/auth/drive");
        _oAuth2->setAuthorizationUrl(  "https://accounts.GOOGLE.com/o/oauth2/auth" );
        _oAuth2->setAccessTokenUrl( "https://oauth2.googleapis.com/token");
        _oAuth2->setClientIdentifier( "the_client_id");
        _oAuth2->setClientIdentifierSharedKey( "the secret_key");
    
        _firstAuth = true;
        _oAuth2->grant();
    
    

    The resulting URL is:

    https://accounts.google.com/o/oauth2/auth?client_id=the_client_id&redirect_uri=http://127.0.0.1:8080/cb&response_type=code&scope=https://www.googleapis.com/auth/drive&state=2hRrr5q1

    There I can add the access_type and approval_prompt to the URL but shouldn't these be included in the QAbstractOAuth2 class ?

    1 Reply Last reply
    1
    • T Offline
      T Offline
      TenG
      wrote on last edited by
      #2

      I found the answer. From this article :

      How To Authenticate with Google SSO in Qt (C++)

      I found that the reply must be assumed to return encoded code, so adding call to setModifyParametersFunction as below fixed the issue:

      _oAuth2 = new QOAuth2AuthorizationCodeFlow( this);
          _oAuth2->setScope( "https://www.googleapis.com/auth/drive");
          _oAuth2->setAuthorizationUrl(  "https://accounts.GOOGLE.com/o/oauth2/auth" );
          _oAuth2->setAccessTokenUrl( "https://oauth2.googleapis.com/token");
          _oAuth2->setClientIdentifier( "the_client_id");
          _oAuth2->setClientIdentifierSharedKey( "the secret_key");
      
      // Added this:
          _oAuth2->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
              // Percent-decode the "code" parameter so Google can match it
              if (stage == QAbstractOAuth::Stage::RequestingAccessToken) {
                  QByteArray code = parameters->value("code").toByteArray();
                  (*parameters)["code"] = QUrl::fromPercentEncoding(code);
              }
          });
      // End Add
      
          _firstAuth = true;
          _oAuth2->grant();```
      Z 1 Reply Last reply
      2
      • T TenG

        I found the answer. From this article :

        How To Authenticate with Google SSO in Qt (C++)

        I found that the reply must be assumed to return encoded code, so adding call to setModifyParametersFunction as below fixed the issue:

        _oAuth2 = new QOAuth2AuthorizationCodeFlow( this);
            _oAuth2->setScope( "https://www.googleapis.com/auth/drive");
            _oAuth2->setAuthorizationUrl(  "https://accounts.GOOGLE.com/o/oauth2/auth" );
            _oAuth2->setAccessTokenUrl( "https://oauth2.googleapis.com/token");
            _oAuth2->setClientIdentifier( "the_client_id");
            _oAuth2->setClientIdentifierSharedKey( "the secret_key");
        
        // Added this:
            _oAuth2->setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
                // Percent-decode the "code" parameter so Google can match it
                if (stage == QAbstractOAuth::Stage::RequestingAccessToken) {
                    QByteArray code = parameters->value("code").toByteArray();
                    (*parameters)["code"] = QUrl::fromPercentEncoding(code);
                }
            });
        // End Add
        
            _firstAuth = true;
            _oAuth2->grant();```
        Z Offline
        Z Offline
        ZOMGDEV
        wrote on last edited by
        #3

        @TenG for QT 6.1.0 this lambda setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) isnt work.
        I got error :
        no viable conversion from lambda to 'const QAbstractOAuth::ModifyParametersFunction' (aka 'const function<void (QAbstractOAuth::Stage, QMultiMap<QString, QVariant> *)>')

        M 1 Reply Last reply
        2
        • Z ZOMGDEV

          @TenG for QT 6.1.0 this lambda setModifyParametersFunction([](QAbstractOAuth::Stage stage, QVariantMap* parameters) isnt work.
          I got error :
          no viable conversion from lambda to 'const QAbstractOAuth::ModifyParametersFunction' (aka 'const function<void (QAbstractOAuth::Stage, QMultiMap<QString, QVariant> *)>')

          M Offline
          M Offline
          Michaelikus
          wrote on last edited by
          #4

          same thing

          1 Reply Last reply
          0
          • franco.amatoF Offline
            franco.amatoF Offline
            franco.amato
            wrote on last edited by
            #5

            Hi how did you fix it? I have the same problem

            SGaistS 1 Reply Last reply
            0
            • franco.amatoF franco.amato

              Hi how did you fix it? I have the same problem

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @franco-amato hi,

              Which version of Qt are you using ?

              The issue of percent encoded value handling has been fixed since then.

              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
              • franco.amatoF Offline
                franco.amatoF Offline
                franco.amato
                wrote on last edited by franco.amato
                #7

                @SGaist I am using Qt 6.7.2, I tried everything and nothing

                m_googleOAuth2->setModifyParametersFunction(
                    [this](QAbstractOAuth::Stage stage, QVariantMap* parameters) {
                        modifyOAuthParameters(stage, parameters);
                    });
                

                void Login::modifyOAuthParameters(QAbstractOAuth::Stage stage, QVariantMap *parameters)
                {
                if (stage == QAbstractOAuth::Stage::RequestingAuthorization)
                {
                parameters->insert("redirect_uri", "http://localhost:8080/");
                }
                }

                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