首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用QOAuth2AuthorizationCodeFlow和QOAuthHttpServerReplyHandler设置redirect_uri

如何使用QOAuth2AuthorizationCodeFlow和QOAuthHttpServerReplyHandler设置redirect_uri
EN

Stack Overflow用户
提问于 2017-03-04 19:03:47
回答 2查看 3K关注 0票数 5

对于使用QT的networkauth和新的QOAuth2AuthorizationCodeFlow对象的OAuth 2.0,如何设置redirect_uri?我的代码如下。它会导致发送以下身份验证url:

QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl:https://accounts.google.com/o/oauth2/auth?client_id=123-abc.apps.googleusercontent.com&redirect_uri=http://localhost:65535/cb&response_type=code&scope=email&state=iEIYn5sN

将redirect_uri设置为"http://localhost",会导致来自谷歌的错误400 redirect_uri_mismatch,这显然是希望提供实际的重定向主机名。

GoogleGateway::GoogleGateway() {

auto google = new QOAuth2AuthorizationCodeFlow;
google->setScope("email");

this->connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);

QString val;
QFile file;
file.setFileName("/home/me/client_secret.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();

QJsonDocument document = QJsonDocument::fromJson(val.toUtf8());
QJsonObject object = document.object();
const auto settingsObject = object["web"].toObject();
const QUrl authUri(settingsObject["auth_uri"].toString());
const auto clientId = settingsObject["client_id"].toString();
const QUrl tokenUri(settingsObject["token_uri"].toString());
const auto clientSecret(settingsObject["client_secret"].toString());
const auto redirectUris = settingsObject["redirect_uris"].toArray();
const QUrl redirectUri(redirectUris[0].toString());
const auto port = static_cast<quint16>(redirectUri.port());

google->setAuthorizationUrl(authUri);
google->setClientIdentifier(clientId);
google->setAccessTokenUrl(tokenUri);
google->setClientIdentifierSharedKey(clientSecret);

auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);
google->setReplyHandler(replyHandler);

google->grant();
}

为了设置redirect_uri,我尝试替换:

auto replyHandler = new QOAuthHttpServerReplyHandler(port, this);

使用

QHostAddress hostaddress = QHostAddress(quint32(1233...));
auto replyHandler = new QOAuthHttpServerReplyHandler(hostaddress, port, this);

结果不会发生变化。

我也尝试过插入:

replyHandler->setProperty("redirect_uri", "http://abc.xyz.com:65535/cb");

结果也不会发生变化。

在Qt/5.8/Src/qtnetworkauth/src/oauth/qoauthhttpserverreplyhandler.cpp,中,我们看到回调地址看起来像是硬编码的:

QString QOAuthHttpServerReplyHandler::callback() const
{
    Q_D(const QOAuthHttpServerReplyHandler);

    Q_ASSERT(d->httpServer.isListening());
    const QUrl url(QString::fromLatin1("http://localhost:%1/cb").arg(d->httpServer.serverPort()));
    return url.toString(QUrl::EncodeDelimiters);
}

此回调()在Qt/5.8/Src/qtnetworkauth/src/oauth/qoauth2authorizationcodeflow.cpp中依次用于设置redirectUri值:

QUrl QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl(const QVariantMap &parameters)
{
    Q_D(QOAuth2AuthorizationCodeFlow);
    using Key = QAbstractOAuth2Private::OAuth2KeyString;

    if (d->state.isEmpty())
        setState(QAbstractOAuth2Private::generateRandomState());
    Q_ASSERT(!d->state.isEmpty());
    const QString state = d->state;

    QVariantMap p(parameters);
    QUrl url(d->authorizationUrl);
    p.insert(Key::responseType, responseType());
    p.insert(Key::clientIdentifier, d->clientCredentials.first);
    p.insert(Key::redirectUri, callback());
    p.insert(Key::scope, d->scope);
    p.insert(Key::state, state);
    if (d->modifyParametersFunction)
        d->modifyParametersFunction(Stage::RequestingAuthorization, &p);
    url.setQuery(d->createQuery(p));
    connect(d->replyHandler.data(), &QAbstractOAuthReplyHandler::callbackReceived, this,
            &QOAuth2AuthorizationCodeFlow::authorizationCallbackReceived, Qt::UniqueConnection);
    setStatus(QAbstractOAuth::Status::NotAuthenticated);
    qDebug("QOAuth2AuthorizationCodeFlow::buildAuthenticateUrl: %s", qPrintable(url.toString()));
    return url;
}

这是一个bug吗?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42595210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档