OAuth2 HTTP メソッドの代替

QtNetworkAuth は、認証されたリクエストを発行するために () のような HTTP メソッドを提供します。OAuth2 の場合、これは通常QAbstractOAuth::getRFC 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 でリクエストファクトリを通常使用します。

© 2025 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.