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 Updated to NodeBB v4.3 + New Features

Log in with access token

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 526 Views 1 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.
  • S Offline
    S Offline
    Sarah_modulo
    wrote on 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.

    Pl45m4P 1 Reply Last reply
    0
    • S Sarah_modulo

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on 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 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

        • Login

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