In questa pagina

Alternative ai metodi HTTP OAuth2

QtNetworkAuth fornisce metodi HTTP quali QAbstractOAuth::get() per l'invio di richieste autenticate. Nel caso di OAuth2, ciò comporta in genere l'impostazione dell'intestazione Authorization, come specificato nella RFC 6750.

Poiché questa operazione è semplice da eseguire, è preferibile utilizzare direttamente le normali API del metodo HTTP QtNetwork e impostare manualmente questa intestazione. Queste API QtNetwork presentano meno vincoli sui tipi di contenuto dei messaggi e offrono una gamma più ampia di API.

Vedi QRestAccessManager, QNetworkAccessManager, QNetworkRequest, QNetworkRequestFactory.

QNetworkRequest

L'intestazione di autorizzazione necessaria può essere impostata direttamente su ogni richiesta che richiede autorizzazione.

using namespace Qt::StringLiterals;

QOAuth2AuthorizationCodeFlow m_oauth;
QNetworkRequest request;

QHttpHeaders headers;
headers.append(QHttpHeaders::WellKnownHeader::Authorization, u"Bearer "_s + m_oauth.token());
request.setHeaders(headers);

Dopo aver impostato l’intestazione, utilizzare la richiesta normalmente con QRestAccessManager o QNetworkAccessManager.

QNetworkRequestFactory

QNetworkRequestFactory è una classe di utilità introdotta in Qt 6.7. Fornisce un metodo adatto a questo compito: QNetworkRequestFactory::setBearerToken(), come illustrato dal codice riportato di seguito.

QNetworkRequestFactory m_api({"https://www.example.com/v3"});
QOAuth2AuthorizationCodeFlow m_oauth;
// ...
connect(&m_oauth, &QOAuth2AuthorizationCodeFlow::granted, this, [this]{
    m_api.setBearerToken(m_oauth.token().toLatin1());
});

Dopo aver impostato il token bearer, utilizzare la factory di richieste normalmente con QRestAccessManager o QNetworkAccessManager.

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.