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. Log in with access token
Forum Update on Monday, May 27th 2025

Log in with access token

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 523 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.
  • S Offline
    S Offline
    Sarah_modulo
    wrote on 15 Dec 2022, 16:52 last edited by
    #1

    I try to access DropBox account in my app. I use QOAuth2AuthorizationCodeFlow.
    I have no problem to do it with authorizeWithBrowser but a webpage open everytime I launch my app and asked for authorization. I would like to use credentials once and after connect to DropBox using Access Token and Refresh Token.
    But I don't understand how it's working !

    Here my code :

    m_auth = new QOAuth2AuthorizationCodeFlow(this);
    
    
        QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(portCallback,this);
        m_auth->setReplyHandler(replyHandler);
    
        m_auth->setAuthorizationUrl(QUrl("https://www.dropbox.com/oauth2/authorize"));
        m_auth->setAccessTokenUrl(QUrl("https://api.dropboxapi.com/oauth2/token"));
        m_auth->setClientIdentifier(apiKey);
        m_auth->setClientIdentifierSharedKey(apiSecret);
        m_auth->setNetworkAccessManager(new QNetworkAccessManager(this));
    
        connect(m_auth, &QAbstractOAuth2::authorizeWithBrowser, [=](QUrl url) {
            QUrlQuery query(url);
    
            query.addQueryItem("force_reapprove", "false"); 
            query.addQueryItem("token_access_type", "offline"); 
            query.addQueryItem("response_type", "code");
    
            url.setQuery(query);
            QDesktopServices::openUrl(url);
        });
    
        connect(m_auth, &QAbstractOAuth::granted, [=](){
            m_isConnected=true;
            m_deviceData->setConnected(true);
            qDebug() << "token : " << m_auth->token() << " expire at : " << m_auth->expirationAt();
            //Can I save it here ? Why should I go through autorizationCallbackReceived ?
        });
    
        connect(m_auth, &QAbstractOAuth2::authorizationCallbackReceived,[=](const QVariantMap data)
        {
            if (false == data.isEmpty())
            {
                QString authCode = data.value("code").toString();
                if(!authCode.isEmpty())
                {
    // What do I need to do here ?
                }
            }
        });
    
        m_auth->grant();
    

    I suppose I need to check/set my access token instead of asked for grant(); Or should I ask for grant in every case but then how the program know that it didn't need to go through all the authorization process ?

    Plus, this is for Access Token but I will need a Refresh Token to reload my Access Token (If I get it right...). So how do I get my refresh token and how do I use it ?

    I hope I'm clear about where I'm lost. I read a lot of document/exemple but they never explain the all mechanism...

    I thanks for any help.

    P 1 Reply Last reply 15 Dec 2022, 17:38
    0
    • S Sarah_modulo
      15 Dec 2022, 16:52

      I try to access DropBox account in my app. I use QOAuth2AuthorizationCodeFlow.
      I have no problem to do it with authorizeWithBrowser but a webpage open everytime I launch my app and asked for authorization. I would like to use credentials once and after connect to DropBox using Access Token and Refresh Token.
      But I don't understand how it's working !

      Here my code :

      m_auth = new QOAuth2AuthorizationCodeFlow(this);
      
      
          QOAuthHttpServerReplyHandler* replyHandler = new QOAuthHttpServerReplyHandler(portCallback,this);
          m_auth->setReplyHandler(replyHandler);
      
          m_auth->setAuthorizationUrl(QUrl("https://www.dropbox.com/oauth2/authorize"));
          m_auth->setAccessTokenUrl(QUrl("https://api.dropboxapi.com/oauth2/token"));
          m_auth->setClientIdentifier(apiKey);
          m_auth->setClientIdentifierSharedKey(apiSecret);
          m_auth->setNetworkAccessManager(new QNetworkAccessManager(this));
      
          connect(m_auth, &QAbstractOAuth2::authorizeWithBrowser, [=](QUrl url) {
              QUrlQuery query(url);
      
              query.addQueryItem("force_reapprove", "false"); 
              query.addQueryItem("token_access_type", "offline"); 
              query.addQueryItem("response_type", "code");
      
              url.setQuery(query);
              QDesktopServices::openUrl(url);
          });
      
          connect(m_auth, &QAbstractOAuth::granted, [=](){
              m_isConnected=true;
              m_deviceData->setConnected(true);
              qDebug() << "token : " << m_auth->token() << " expire at : " << m_auth->expirationAt();
              //Can I save it here ? Why should I go through autorizationCallbackReceived ?
          });
      
          connect(m_auth, &QAbstractOAuth2::authorizationCallbackReceived,[=](const QVariantMap data)
          {
              if (false == data.isEmpty())
              {
                  QString authCode = data.value("code").toString();
                  if(!authCode.isEmpty())
                  {
      // What do I need to do here ?
                  }
              }
          });
      
          m_auth->grant();
      

      I suppose I need to check/set my access token instead of asked for grant(); Or should I ask for grant in every case but then how the program know that it didn't need to go through all the authorization process ?

      Plus, this is for Access Token but I will need a Refresh Token to reload my Access Token (If I get it right...). So how do I get my refresh token and how do I use it ?

      I hope I'm clear about where I'm lost. I read a lot of document/exemple but they never explain the all mechanism...

      I thanks for any help.

      P Offline
      P Offline
      Pl45m4
      wrote on 15 Dec 2022, 17:38 last edited by Pl45m4
      #2

      @Sarah_modulo

      Hi,

      so you want to login once with your account and then use the token to authenticate until you log out?!

      There is this post on StackOverflow

      • https://stackoverflow.com/questions/46907896/google-oauth-2-0-refresh-token-persistence-qt

      where this libary is recommended. Seems to work for OP
      (if you read the manual on GitHub, there is a section, how to store the token persistently for later use)

      • https://github.com/pipacs/o2

      Unfortunately I dont know exacly whether it's possible using QAuth only and if so, how.

      Hope, it helps :)


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      1
      • S Offline
        S Offline
        Sarah_modulo
        wrote on 19 Dec 2022, 08:49 last edited by
        #3

        Thanks but I don't want to import any library.
        I will check if i understand it better in the code of this library, but for know it's still very confusing.

        1 Reply Last reply
        0

        2/3

        15 Dec 2022, 17:38

        • Login

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