Qt6: How to solve the qt.networkauth.oauth2: Unexpected call error on oAuth

297 Views Asked by At

I have a code to connect to Google drive using QT6 but I keep getting the error qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: qt.networkauth.oauth2: Unexpected call

I have even implemented the suggested changes mentioned here Can't get OAuth 2.0 code in Qt app, but is seems to work in browser. This seem to have changed the encoding of code as expected but the end result is the same. I keep getting the same error. What am I missing?

Google::GoogleCon(QObject *parent) : QObject(parent),
    m_networkAccessManager(new QNetworkAccessManager(this)),
    m_networkReply(nullptr),
    m_dataBuffer(new QByteArray)
{

    emit showBusyIndicator(true);

    this->google = new QOAuth2AuthorizationCodeFlow(this);

    this->google->setScope("email https://www.googleapis.com/auth/drive");
    connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl);

    this->google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth"));
    this->google->setClientIdentifier(<CLIENT>);
    this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token"));
    this->google->setClientIdentifierSharedKey(<SECRET>);


    this->google->setModifyParametersFunction([this](QAbstractOAuth::Stage stage, QMultiMap<QString, QVariant>* parameters)
    {
        qDebug() << "modifyParametersFunction stage=" << static_cast<int>(stage);
        if (stage == QAbstractOAuth::Stage::RequestingAuthorization)
        {
            parameters->insert("access_type", "offline");
            parameters->insert("prompt", "consent");
        }
        else if (stage == QAbstractOAuth::Stage::RequestingAccessToken)
        {
            QByteArray code = parameters->value("code").toByteArray();
            QString codeString = QUrl::fromPercentEncoding(code);


            parameters->replace("code", codeString);

        }
    });

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

    connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=]() {
        qDebug() << "Access Granted!";
    });
}
0

There are 0 best solutions below