connection with google on Qt
-
I'm coming to you because I'm facing a problem that I can't solve.
Here is my problem: for a personal project on Qt, I want to integrate the connection with google (OAuth), here is my code:
#include "controleur.h" #include "ui_controleur.h" #include <QOAuth2AuthorizationCodeFlow> #include <QDesktopServices> #include <QJsonDocument> #include <QOAuthHttpServerReplyHandler> #include <QJsonDocument> #include <QJsonObject> #include <QJsonArray> #include <QString> #include <QFile> #include <QDir> #include <QUrl> #include <QNetworkReply> #include <QAbstractOAuth> #include <QVariantMap> Controleur::Controleur(QWidget *parent) : QDialog(parent) , ui(new Ui::Controleur) { ui->setupUi(this); } Controleur::~Controleur() { delete ui; } void Controleur::on_pushButton_released() { auto google = new QOAuth2AuthorizationCodeFlow; google->setScope("email"); connect(google, &QOAuth2AuthorizationCodeFlow::authorizeWithBrowser,&QDesktopServices::openUrl); connect(google, &QOAuth2AuthorizationCodeFlow::granted, [=](){ //qDebug() << __FUNCTION__ << __LINE__ << "Access Granted!"; auto reply = 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(); }); }); QByteArray val; QFile file; file.setFileName(QDir::toNativeSeparators("C:/Users/USER/Downloads/code_secret_client_xxxxxxxxxx.apps.googleusercontent.com.json")); if(file.open(QIODevice::ReadOnly | QIODevice::Text)) { val = file.readAll(); file.close(); } QJsonDocument document = QJsonDocument::fromJson(val); const auto 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()); // Get the first URI const auto port = static_cast<quint16>(redirectUri.port()); // Get the port google->setAuthorizationUrl(authUri); google->setClientIdentifier(clientId); google->setAccessTokenUrl(tokenUri); google->setClientIdentifierSharedKey(clientSecret); auto replyHandler = new QOAuthHttpServerReplyHandler(port, this); qDebug() << replyHandler->isListening(); google->setReplyHandler(replyHandler); google->grant(); }
when we click on the button everything goes well, the browser also opens without any problem, the user can also select a Google account for the connection to my application, and after the choice of the account by the user here is the message that is displayed in my browser: "Callback received. Feel free to close this page".
but when I look in the console of my application, here are the messages that are displayed: "true" "qt.networkauth.oauth2: Unexpected call" "qt.networkauth.replyhandler: Error transferring https://oauth2.googleapis.com/token - server replied: "
which proves that the browser is not able to transmit the data to my application and that's where I'm stuck
Please tell me where my error is? because I want my application to receive the data from the google account selected by the user in the browser.
-
Hi,
Might be a silly question but are you sure the callback URL is correct ?
-
@EL-jos said in connection with google on Qt:
qt.networkauth.oauth2: Unexpected call
This stack overflow thread might help you.
-
@EL-jos said in connection with google on Qt:
qt.networkauth.oauth2: Unexpected call
This stack overflow thread might help you.
-
If you provide a minimal compilable example, it will be easier to test.
-
Your code as is not reusable as you are loading a json file from your hard drive that contains some secrets and some standard information (by the way the toNativeSeparators call is unnecessary as Qt supports forward slashes on all platform and will do the transformation when/if needed).
It's also not including the designer .ui file.
Hence a minimal compilable example is something that can be directly built minus the secrets information like client_id and other token that shall be changed by the person testing your code. You can put a placeholder text where these secret values shall be changed.
-
Your code as is not reusable as you are loading a json file from your hard drive that contains some secrets and some standard information (by the way the toNativeSeparators call is unnecessary as Qt supports forward slashes on all platform and will do the transformation when/if needed).
It's also not including the designer .ui file.
Hence a minimal compilable example is something that can be directly built minus the secrets information like client_id and other token that shall be changed by the person testing your code. You can put a placeholder text where these secret values shall be changed.
@SGaist here is the content of my json file
{"web":{"client_id":"<snip>","project_id":"qt-connexion","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"<snip>","redirect_uris":["http://localhost:5476/"],"javascript_origins":["http://localhost","http://localhost:5476"]}}
-
Did you saw this updated version of the example code you likely used for your application:
https://appfluence.com/productivity/how-to-authenticate-qt-google-sso/
?