OAuth2のHTTPメソッドの代替手段
QtNetworkAuth では、認証済みリクエストを発行するためのQAbstractOAuth::get() などの HTTP メソッドが提供されています。OAuth2 の場合、これは通常、RFC 6750 で規定されているように、Authorization ヘッダーを設定することを意味します。
この処理は簡単に行えるため、通常のQtNetwork HTTPメソッドAPIを直接使用し、このヘッダーを手動で設定することをお勧めします。これらのQtNetwork APIは、メッセージのコンテンツタイプに対する前提条件が少なく、より幅広いAPIセットを提供します。
QRestAccessManager 、QNetworkAccessManager 、QNetworkRequest 、QNetworkRequestFactory を参照してください。
QNetworkRequest
必要なAuthorizationヘッダーは、認証が必要な各リクエストで直接設定できます。
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);ヘッダーを設定した後、QRestAccessManager またはQNetworkAccessManager を使用して、通常通りリクエストを実行してください。
QNetworkRequestFactory
QNetworkRequestFactory は、Qt 6.7 で導入された利便性向上のためのクラスです。このクラスには、以下のコードに示すように、このタスクに適したメソッド `QNetworkRequestFactory::setBearerToken()` が用意されています。
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());
});ベアラー・トークンを設定した後、QRestAccessManager または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.