Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error 400: redirect_uri_mismatch using google OAuth
Forum Updated to NodeBB v4.3 + New Features

Error 400: redirect_uri_mismatch using google OAuth

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DeadSo0ul
    wrote on last edited by DeadSo0ul
    #1

    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
    Screenshot 2024-04-27 at 14.17.24.png
    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/
    
    C 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but did you check the the QUrl object containing the redirect URI contains the correct value ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi,

        Might be a silly question but did you check the the QUrl object containing the redirect URI contains the correct value ?

        D Offline
        D Offline
        DeadSo0ul
        wrote on last edited by
        #3

        Hi,
        I did, with

            const QUrl redirectUri(redirectUris[0].toString());
            qDebug() << redirectUri;
        

        It appears to be coorect

        QUrl("http://localhost:8080/cb")
        
        SGaistS 1 Reply Last reply
        0
        • D DeadSo0ul

          Hi,
          I did, with

              const QUrl redirectUri(redirectUris[0].toString());
              qDebug() << redirectUri;
          

          It appears to be coorect

          QUrl("http://localhost:8080/cb")
          
          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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).

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • D DeadSo0ul

            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
            Screenshot 2024-04-27 at 14.17.24.png
            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/
            
            C Offline
            C Offline
            ChrisW67
            wrote on last edited by
            #5

            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?

            D 1 Reply Last reply
            0
            • C ChrisW67

              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?

              D Offline
              D Offline
              DeadSo0ul
              wrote on last edited by
              #6

              @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

              SGaistS 1 Reply Last reply
              0
              • D DeadSo0ul

                @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

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @DeadSo0ul Might be a silly question but did you also change the Google settings to use 127.0.0.1 in place of localhost ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                0
                • SGaistS SGaist

                  @DeadSo0ul Might be a silly question but did you also change the Google settings to use 127.0.0.1 in place of localhost ?

                  D Offline
                  D Offline
                  DeadSo0ul
                  wrote on last edited by
                  #8

                  Hi, Sorry for the late reply. I did change the google side to 127.0.0.1 today and the error changed a bit

                  Application details: redirect_uri = http://127.0.0.1:0/
                  
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    That's pretty strange... it's the first time I see port 0 being added... what if you set the port to 80 explicitly ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      DeadSo0ul
                      wrote on last edited by
                      #10

                      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/
                      
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved