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

Error 400: redirect_uri_mismatch using google OAuth

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.1k Views
  • 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 27 Apr 2024, 11:20 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 29 Apr 2024, 04:41
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 27 Apr 2024, 19:38 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 27 Apr 2024, 20:14
      0
      • S SGaist
        27 Apr 2024, 19:38

        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 27 Apr 2024, 20:14 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")
        
        S 1 Reply Last reply 28 Apr 2024, 19:48
        0
        • D DeadSo0ul
          27 Apr 2024, 20:14

          Hi,
          I did, with

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

          It appears to be coorect

          QUrl("http://localhost:8080/cb")
          
          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 Apr 2024, 19:48 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
            27 Apr 2024, 11:20

            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 29 Apr 2024, 04:41 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 29 Apr 2024, 10:41
            0
            • C ChrisW67
              29 Apr 2024, 04:41

              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 29 Apr 2024, 10:41 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

              S 1 Reply Last reply 29 Apr 2024, 20:03
              0
              • D DeadSo0ul
                29 Apr 2024, 10:41

                @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

                S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 29 Apr 2024, 20:03 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 4 May 2024, 07:58
                0
                • S SGaist
                  29 Apr 2024, 20:03

                  @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 4 May 2024, 07:58 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 4 May 2024, 18:23 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 6 May 2024, 14:45 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

                      6/10

                      29 Apr 2024, 10:41

                      • Login

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