Google OAuth gives error when trying to exchange token
Solved
General and Desktop
-
Hi, I can't seem to make the google oauth 2.0 connect with
QNetworkAccessManager
#include "googlegateway.hpp" #include <QObject> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QString> #include <QFile> #include <QDir> #include <QUrl> #include <QOAuthHttpServerReplyHandler> #include <QDesktopServices> #include <QEventLoop> GoogleGateway::GoogleGateway(QObject* parent) : QObject(parent) { google = new QOAuth2AuthorizationCodeFlow; // Create a new network access manager and assign it to the OAuth object QNetworkAccessManager* networkManager = new QNetworkAccessManager(this); google->setNetworkAccessManager(networkManager); // Assign the manager to google google->setScope("profile email openid"); QObject::connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, &QDesktopServices::openUrl); google->setClientIdentifier("user.apps.googleusercontent.com"); google->setClientIdentifierSharedKey("key"); google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth")); google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token")); // Initialize the reply handler with a specific port, if necessary replyHandler = new QOAuthHttpServerReplyHandler(1234, this); google->setReplyHandler(replyHandler); // Grant access and handle token reception google->grant(); QObject::connect(google, &QOAuth2AuthorizationCodeFlow::granted, [this] { qDebug() << "Token granted:" << google->token(); }); // Optionally connect the network manager's signals for further debugging QObject::connect(networkManager, &QNetworkAccessManager::finished, this, [](QNetworkReply* reply) { qDebug() << "Request URL:" << reply->url().toString(); qDebug() << "Request status code:" << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qDebug() << "Raw headers:" << reply->rawHeaderList(); if (reply->error() != QNetworkReply::NoError) { qDebug() << "Request failed with error:" << reply->errorString(); qDebug() << "Response:" << reply->readAll(); // This logs the server response body } else { qDebug() << "Request succeeded. Response:" << reply->readAll(); } }); }
Here is my output:
qt.networkauth.replyhandler: Invalid request: /favicon.ico Request URL: "https://oauth2.googleapis.com/token" Request status code: 400 Raw headers: QList("date", "pragma", "cache-control", "expires", "content-type", "vary", "server", "x-xss-protection", "x-frame-options", "x-content-type-options", "alt-svc", "accept-ranges") Request failed with error: "Error transferring https://oauth2.googleapis.com/token - server replied: " Response: "{\n \"error\": \"invalid_grant\",\n \"error_description\": \"Malformed auth code.\"\n}" qt.networkauth.oauth2: Token request failed: "Error transferring https://oauth2.googleapis.com/token - server replied: "
I will leave a screenshot of the google console
-
I managed to fix it using google drive.
#include "googleauth.h" GoogleAuth::GoogleAuth(QObject *parent) : QObject(parent) { this->google = new QOAuth2AuthorizationCodeFlow(this); // Set Scope or Permissions required from Google this->google->setScope("email profile openid https://www.googleapis.com/auth/drive.readonly"); connect(this->google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser, [=](QUrl url) { QUrlQuery query(url); query.addQueryItem("prompt", "consent"); // Param required to get data everytime query.addQueryItem("access_type", "offline"); // Needed for Refresh Token (as AccessToken expires shortly) url.setQuery(query); QDesktopServices::openUrl(url); }); // OAuth URLs and Client Identifier (from Google Console) this->google->setAuthorizationUrl(QUrl("https://accounts.google.com/o/oauth2/auth")); this->google->setAccessTokenUrl(QUrl("https://oauth2.googleapis.com/token")); this->google->setClientIdentifier("user.apps.googleusercontent.com"); this->google->setClientIdentifierSharedKey("client_secret"); auto replyHandler = new QOAuthHttpServerReplyHandler(5476, this); this->google->setReplyHandler(replyHandler); connect(this->google, &QOAuth2AuthorizationCodeFlow::granted, [=]() { qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!"; // Fetching user info from OpenID Connect endpoint QUrl userInfoUrl("https://www.googleapis.com/oauth2/v3/userinfo"); auto reply = this->google->get(userInfoUrl); connect(reply, &QNetworkReply::finished, [reply]() { if (reply->error() == QNetworkReply::NoError) { qDebug() << "User Info: " << reply->readAll(); } else { qDebug() << "Error fetching user info: " << reply->errorString(); } reply->deleteLater(); }); }); } // Call this function to prompt authentication // and receive data from Google void GoogleAuth::click() { this->google->grant(); }
Absolutely no idea why it needs google drive. I would like it to work without it so if anyone has an idea what could be done. Let me know. Thanks
-