Help with Google Sign-In
-
I have to implement the Google Sign-In in my desktop application (I am using Qt 6.7.2).
I added a googleLoginButton button with the following slot:void Login::on_googleLoginButton_clicked() { m_googleOAuth2->grant(); }In the ctor of my class I setup the Google Auth
Login::Login(QWidget *parent) : QDialog(parent), ui(new Ui::Login) { ui->setupUi(this); setupGoogleAuth(); }Here the google auth setup
void Login::setupGoogleAuth() { m_googleOAuth2 = new QOAuth2AuthorizationCodeFlow(this); // Set client credentials m_googleOAuth2->setClientIdentifier("947359679464-jq8q0nscv9nioa3ig4lobrcsoc2tlpos.apps.googleusercontent.com"); m_googleOAuth2->setClientIdentifierSharedKey("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"); // Set authorization and token URLs m_googleOAuth2->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth")); m_googleOAuth2->setAccessTokenUrl(QUrl("https://accounts.google.com/o/auth2/token")); // Set the scopes m_googleOAuth2->setScope("openid email profile"); // Set the reply handler m_replyHandler = new QOAuthHttpServerReplyHandler(1337, this); m_googleOAuth2->setReplyHandler(m_replyHandler); // Connect signals and slots connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl); connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::granted, this, &Login::onGoogleLoginSuccess); connect(m_googleOAuth2, &QOAuth2AuthorizationCodeFlow::error, this, [this](const QString &error, const QString &errorDescription, const QUrl &uri) { onGoogleLoginError(error, errorDescription, uri); }); }In case of success
void Login::onGoogleLoginSuccess() { qDebug() << "AMATO: Google login successful"; QString accessToken = m_googleOAuth2->token(); // Save the access token securely saveAccessToken(accessToken); // Fetch user profile fetchGoogleUserProfile(); QJsonObject requestData; requestData["access_token"] = accessToken; auto mgr = new QNetworkAccessManager(this); QNetworkReply *reply = BBHelper::HttpRequest(POST, mgr, "/api/google/login", &requestData); connect(reply, &QNetworkReply::finished, this, [this, reply]() { handleBackendResponse(reply); }); }In case of error:
void Login::onGoogleLoginError(const QString &error, const QString &errorDescription, const QUrl &uri) { qWarning() << "AMATO: Google login error:" << error << errorDescription << uri.toString(); QMessageBox::warning(this, "Login Error", "Failed to log in with Google " + errorDescription); }Unfortunately neither onGoogleLoginSuccess nor onGoogleLoginError is called and I cannot understand why. The cliend_id and client_secret are correct.
I would like to understand what I am doing wrong -
Don't see where you start the whole thing: https://doc.qt.io/qt-6/qoauth2authorizationcodeflow.html#grant
-
@Christian-Ehrlicher here:
void Login::on_googleLoginButton_clicked()
{
m_googleOAuth2->grant();
} -
And do you get there?
-
When I press the button, a web page opens so that i can choose the google account but after that nothing happens
-
@SGaist The connection is already there. Now I could solve some problems but I noticed that I cannot get the token if I set the scope in this way:
m_oauth->setScope("email"); or m_oauth->setScope("https://www.googleapis.com/auth/userinfo.email");My only purpose is to get the user email. To get it working I have to set the scope in this way:
m_oauth->setScope("email https://www.googleapis.com/auth/drive.readonly");I added to enable the drive api for that. For the first case I enabled People API and nothing. I cannot understand the problem
-
Did you set the required scopes in your Google OAuth application ?