Error 400: redirect_uri_mismatch using google OAuth
-
Hi, I am trying to connect google's oauth to my app but I face a mismatch between the redirect_uri
Here is my code:#pragma once #include <QMainWindow> #include <QOAuth2AuthorizationCodeFlow> #include "googlegateway.hpp" QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_pushButton_clicked(); private: Ui::MainWindow *ui; GoogleGateway* google; };
#include "googlegateway.hpp" #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("/Users/boyankiovtorov/Downloads/auth.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["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()); 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(port, 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(); }); }); }
The Authorized redirect URI in the google cloud console is
http://localhost:8080/cb
I downloaded the json from google and so the redirect uri is the same
"redirect_uris":["http://localhost:8080/cb"]
But I still get error when trying to connect to google
Error 400: redirect_uri_mismatch You cannot log in to this app because it does not comply with Google's OAuth 2.0 rules. If you are the programmer of the application, register the Google Cloud Console URI for redirect. Application details: redirect_uri = http://127.0.0.1:8080/
-
Hi,
Might be a silly question but did you check the the QUrl object containing the redirect URI contains the correct value ?
-
@DeadSo0ul looks like something funky is going on the Google side. What happens if you remove cb when doing the request ? (I know, it's not intuitive but might lead to a new message more helpful).
-
This:
"redirect_uris":["http://localhost:8080/cb"]
may be considered by Google to not match this:
Application details: redirect_uri = http://127.0.0.1:8080/
Have you tried using 127.0.0.1 in place of localhost?
-
@ChrisW67
I did"redirect_uris":["http://127.0.0.1:8080/cb"],"javascript_origins":["http://127.0.0.1:8080"]
Same error on the google side
Application details: redirect_uri = http://127.0.0.1:8080/
I also tried to change
,"redirect_uris":["http://127.0.0.1:8080/"],"javascript_origins":["http://127.0.0.1:8080"]}}
(removed the cb)
but the error stays the same
-
@DeadSo0ul Might be a silly question but did you also change the Google settings to use
127.0.0.1
in place oflocalhost
? -
That's pretty strange... it's the first time I see port 0 being added... what if you set the port to 80 explicitly ?
-
I set the port to 80 in the cloud console. Now it looks like
http://127.0.0.1:80/cb
the qDebug() says
QUrl("http://127.0.0.1:80/cb")
but the error on google side stays the same
Error 400: redirect_uri_mismatch You cannot log in to this app because it does not comply with Google's OAuth 2.0 rules. If you are the programmer of the application, register the Google Cloud Console URI for redirect. Application details: redirect_uri = http://127.0.0.1:0/