将 google API 连接到 QT 应用程序的问题

问题描述 投票:0回答:1

我使用以下博客尝试使用谷歌功能对我的应用程序实施登录: https://www.qt.io/blog/2017/01/25/connecting-qt-application-google-services-using-oauth-2-0

我创建了一个类来处理这个过程,我将链接下面的代码。 到目前为止,代码工作到我可以按下 GUI 按钮,我被重定向到我的浏览器以登录,但是当我检查调试器时,会看到以下错误,并且应用程序似乎没有收到任何身份验证:

qt.networkauth.oauth2:意外调用 qt.networkauth.replyhandler:传输错误 https://oauth2.googleapis.com/token - 服务器回复:

这是我编写的代码:

#include "googleapi.h"
#include <QObject>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QString>
#include <QFile>
#include <QDir>
#include <QUrl>
#include <QOAuthHttpServerReplyHandler>
#include <QDesktopServices>


GoogleGateway::GoogleGateway(QObject* parent) : QObject(parent)
{
    this->google = new QOAuth2AuthorizationCodeFlow(this);
    this->google->setScope("email");

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

    QByteArray val;
    QFile file;
    file.setFileName(QDir::toNativeSeparators("/path/credential_file.json"));
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        val = file.readAll();
        file.close();
    }


    QJsonDocument document = QJsonDocument::fromJson(val);
    QJsonObject object = document.object();
    const auto settingsObject = object["installed"].toObject();
    //qDebug() << settingsObject["auth_uri"].toString() << settingsObject["client_id"].toString() << settingsObject["token_uri"].toString() << settingsObject["client_secret"].toString();

    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());

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

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

   
    connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=]() {
        qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!";
    auto reply = this->google->get(QUrl("https://www.googleapis.com/plus/v1/people/me"));
    connect(reply, &QNetworkReply::finished, [reply]() {
        qDebug() << "REQUEST FINISHED. Error? " << (reply->error() != QNetworkReply::NoError);
    qDebug() << reply->readAll();
        });
        });
   
}

我尝试检查我的网络连接,我尝试检查重定向 uri 和其他凭据,我尝试使用 &QOAuth2AuthorizationCodeFlow::error 使用连接语句,但它也没有用。

c++ qt oauth google-api q
1个回答
0
投票

如果你在qt6中试试这组连接

// Connect the signals to retrieve the tokens, if you want to see them
connect(google, &QOAuth2AuthorizationCodeFlow::tokenChanged, [=](const QString &token)
        {
            qDebug() << "Token changed:" << token;

        });
connect(replyHandler, &QOAuthHttpServerReplyHandler::tokensReceived, [](const QVariantMap &tokens)
        {
            qDebug() << "Tokens received:" << tokens;
        });

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

// Once you are granted access, make a request via the Google API
connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=]
        {
            qDebug() << "in the granted block now";
            // This block is run once you have logged into the browser successfully

            auto rep = google->get(QUrl("https://www.googleapis.com/oauth2/v1/userinfo?alt=json"));
            QEventLoop loop;
            connect(rep, &QNetworkReply::finished, &loop, &QEventLoop::quit);
            loop.exec();
            QString currentByteArray = rep->readAll();
            qDebug() << "network reply google api connect step get info" << currentByteArray;

        });
© www.soinside.com 2019 - 2024. All rights reserved.