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. QNetworkRequest basic Authentication

QNetworkRequest basic Authentication

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 3.1k Views 3 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.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by
    #1

    My curl command looks like this:

    curl --user 'user:password' --data '{"jsonrpc": "1.0", "id":"curltest", "method": "list", "params": [6, 9999999, [] , true, { "minimumAmount": 0.005 } ] }' -H 'content-type: application/json;' http://127.0.0.1:8332
    

    If I use this curl command from the console it works fine.

    For QNetworkManager I tried like this:

    QByteArray userAndPassword;
    userAndPassword.append(m_rpcUser);
    userAndPassword.append(":");
    userAndPassword.append(m_rpcPassword);
    
    m_networkRequest.setRawHeader("Authorization", "Basic " + userAndPassword);
    m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    

    I always get the following error:

    Error QNetworkReply::AuthenticationRequiredError "Der Host verlangt eine Authentifizierung"
    

    How can I set the basic authentication for a QNetworkRequest?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You forgot to encode the credentials.

      See here.

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

      I 1 Reply Last reply
      0
      • SGaistS SGaist

        You forgot to encode the credentials.

        See here.

        I Offline
        I Offline
        Infinity
        wrote on last edited by Infinity
        #3

        @SGaist said in QNetworkRequest basic Authentication:

        You forgot to encode the credentials.

        Now it looks like this:

        QString credentialsString = QString("%1:%2").arg(m_rpcUser).arg(m_rpcPassword);
        QByteArray credentialsArray = credentialsString.toLocal8Bit().toBase64();
        QString headerData = "Basic " + credentialsArray;
        
        m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
        

        But I still get the same error.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          One other thing you can use is the QNAM::authenticationRequired signal and fill the details there.

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

          I 1 Reply Last reply
          0
          • SGaistS SGaist

            One other thing you can use is the QNAM::authenticationRequired signal and fill the details there.

            I Offline
            I Offline
            Infinity
            wrote on last edited by Infinity
            #5

            @SGaist said in QNetworkRequest basic Authentication:

            One other thing you can use is the QNAM::authenticationRequired signal and fill the details there.

            With this I don't get the error anymore, but the the request is not called anymore.

            void RpcEndPoint::on_networkManagerAuthenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
            {
                authenticator->setUser(m_rpcUser);
                authenticator->setPassword(m_rpcPassword);
            
                m_reply = reply;
            
                // Connect the slots to the new m_reply
                connect(m_reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error),
                    [=](QNetworkReply::NetworkError code) {
                        qDebug() << "Error" << code << m_reply->errorString();
                    }
                );
            
                connect(m_reply, &QNetworkReply::downloadProgress,
                        this, &RpcEndPoint::on_downloadProgress);
            
                connect(m_reply, &QNetworkReply::readyRead,
                        this, &RpcEndPoint::on_downloadReadyRead);
            
                connect(m_reply, &QNetworkReply::finished,
                        this, &RpcEndPoint::on_downloadFinished);
            
            }
            
            1 Reply Last reply
            0
            • I Infinity

              @SGaist said in QNetworkRequest basic Authentication:

              You forgot to encode the credentials.

              Now it looks like this:

              QString credentialsString = QString("%1:%2").arg(m_rpcUser).arg(m_rpcPassword);
              QByteArray credentialsArray = credentialsString.toLocal8Bit().toBase64();
              QString headerData = "Basic " + credentialsArray;
              
              m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());
              

              But I still get the same error.

              Pablo J. RoginaP Offline
              Pablo J. RoginaP Offline
              Pablo J. Rogina
              wrote on last edited by
              #6

              @Infinity said in QNetworkRequest basic Authentication:

              m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());

              It looks like you're missing the final ":" (colon) for the header, as it should be "Authentication:" see RFC 7617.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              I 1 Reply Last reply
              0
              • Pablo J. RoginaP Pablo J. Rogina

                @Infinity said in QNetworkRequest basic Authentication:

                m_networkRequest.setRawHeader("Authorization", headerData.toLocal8Bit());

                It looks like you're missing the final ":" (colon) for the header, as it should be "Authentication:" see RFC 7617.

                I Offline
                I Offline
                Infinity
                wrote on last edited by
                #7

                @Pablo-J-Rogina said in QNetworkRequest basic Authentication:

                It looks like you're missing the final ":" (colon) for the header, as it should be "Authentication:" see RFC 7617.

                QString credentialsString = QString("%1:%2").arg(m_rpcUser).arg(m_rpcPassword);
                
                QByteArray credentialsArray = credentialsString.toLocal8Bit().toBase64();
                
                QString headerData = "Basic " + credentialsArray;
                
                m_networkRequest.setRawHeader("Authentication:", headerData.toLocal8Bit());
                

                Still the same error with this

                1 Reply Last reply
                0
                • I Offline
                  I Offline
                  Infinity
                  wrote on last edited by Infinity
                  #8

                  I found the following solution:

                  m_endPointUrl.setUserName(m_rpcUserName);
                  m_endPointUrl.setPassword(m_rpcPassword);
                  
                  m_networkRequest.setUrl(m_endPointUrl);
                  m_networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"))
                  
                  m_reply = m_networkManager->post(m_networkRequest, requestJsonDocument.toJson(QJsonDocument::Compact));
                  

                  But I still would like to know how it would work with:

                  QNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *authenticator)
                  
                  1 Reply Last reply
                  1

                  • Login

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