OAuth2 HTTP 메서드 대체

QtNetworkAuth 는 인증된 요청을 발행하기 위해 QAbstractOAuth::get()와 같은 HTTP 메서드를 제공합니다. OAuth2의 경우 일반적으로 RFC 6750에 명시된 대로 Authorization 헤더를 설정하는 것을 의미합니다.

이 작업은 간단하기 때문에 일반적인 QtNetwork HTTP 메서드 API를 직접 사용하고 이 헤더를 수동으로 설정하는 것이 좋습니다. 이러한 QtNetwork API는 메시지 콘텐츠 유형에 대한 가정이 적고 더 광범위한 API를 제공합니다.

QRestAccessManager, QNetworkAccessManager, QNetworkRequest, QNetworkRequestFactory 을 참조하세요.

QNetworkRequest

필요한 권한 부여 헤더는 권한 부여가 필요한 각 요청에 직접 설정할 수 있습니다.

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.