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::refreshToken() returns an empty string when the QOAuth2AuthorizationCodeFlow::granted signal is emitted.
QtWS25 Last Chance

QAbstractOAuth2::refreshToken() returns an empty string when the QOAuth2AuthorizationCodeFlow::granted signal is emitted.

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 196 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.
  • H Offline
    H Offline
    Hossein2000
    wrote on last edited by
    #1

    In the code below, the authentication process is performed by QOAuth2AuthorizationCodeFlow.
    This code works correctly to get the access token, but it does not return the refresh token.

    #include "qoauth2test.h"
    QOAuth2Test::QOAuth2Test(QObject *parent) : QObject(parent)
    {
        // Launch browser for authorization request
        connect(&google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
                &QDesktopServices::openUrl);
        // Receive answer from authorization request process
        connect(&google, &QOAuth2AuthorizationCodeFlow::granted, this, &QOAuth2Test::onGranted);
    
        QString authUri = "https://accounts.google.com/o/oauth2/v2/auth";
        QString clientId = "XXXX";
        QString tokenUri = "https://accounts.google.com/o/oauth2/token";
        QString clientSecret = "XXXX";
        QString scope = "https://www.googleapis.com/auth/drive";
        quint16 port = 8080;
        google.setAuthorizationUrl(authUri);
        google.setClientIdentifier(clientId);
        google.setAccessTokenUrl(tokenUri);
        google.setClientIdentifierSharedKey(clientSecret);
        google.setScope(scope);
    
        google.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);
            }
        });
    
        auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
        google.setReplyHandler(replyHandler);
    }
    
    void QOAuth2Test::grant()
    {
        google.grant();
    }
    
    void QOAuth2Test::onGranted()
    {
        accessToken = google.token();
        qInfo() << "access Token:"<< accessToken;
        refreshToken = google.refreshToken();
        qInfo() << "refresh Token:"<< refreshToken;
    }
    

    Please tell me if anyone knows the solution.
    Thank you.

    1 Reply Last reply
    0
    • H Offline
      H Offline
      Hossein2000
      wrote on last edited by
      #2

      I found the answer
      You need to replace

      connect(&google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,
                  &QDesktopServices::openUrl);
      

      with

      connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [=](QUrl url) {
              QUrlQuery query(url);
      
              query.addQueryItem("prompt", "consent");      // Param required to get data everytime
              query.addQueryItem("access_type", "offline"); // Needed for Refresh Token (as AccessToken expires shortly)
              url.setQuery(query);
              QDesktopServices::openUrl(url);
          });
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Glad you found a solution and thanks for sharing !

        Would you mind explaining how you got the solution ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        H 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          Glad you found a solution and thanks for sharing !

          Would you mind explaining how you got the solution ?

          H Offline
          H Offline
          Hossein2000
          wrote on last edited by
          #4

          @SGaist
          Thanks . you're welcome
          I stumbled this webpage accidentally, where Mr. Chilarai Mushahary had done exactly what I was looking for.

          1 Reply Last reply
          0
          • H Hossein2000 has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved