Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [SOLVED] QWebview - website requires authentication
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] QWebview - website requires authentication

Scheduled Pinned Locked Moved Qt WebKit
12 Posts 7 Posters 18.6k 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.
  • M Offline
    M Offline
    mark.eastonregenersis.com
    wrote on 1 May 2012, 21:04 last edited by
    #1

    Hi,

    I am developing an application that uses the 'QWebview' widget to display a website on the internet.

    My problem is, the website I am trying to load has a authentication popup window (as soon as you load the page) so you can enter a username and password - but when using the Webview widget, it just displays the following error message:

    "Authorization Required. This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required".

    Is there a way round this?

    My code:
    @
    QWebview *m_pWebView;
    m_pWebView->url().setUserName("username");
    m_pWebView->url().setPassword("password");
    m_pWebView->load(QUrl("website"));
    m_pWebView->show();
    @

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tobias.hunger
      wrote on 2 May 2012, 06:57 last edited by
      #2

      Looks like encoding user/password into the url does not work.

      Basically that works for websites that have the browser pop up that its own login window (HTTP basic auth IIRC), but not for websites that have username/password fields somewhere on their webpage.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mark.eastonregenersis.com
        wrote on 2 May 2012, 15:34 last edited by
        #3

        What about QNetworkAccessManager?

        Trying to get this method to work but I am getting no such SLOT errors!

        @QNetworkAccessManager manager = new QNetworkAccessManager();
        connect(manager,SIGNAL(authenticationRequired(QNetworkReply reply
        , QAuthenticator auth*)),SLOT(provideAuthentication(QNetworkReply *reply,QAuthenticator *auth)));@

        @void MainWindow::provideAuthentication(QNetworkReply *reply, QAuthenticator *auth)
        {
        auth->setUser("username");
        auth->setPassword("password");
        }@

        1 Reply Last reply
        0
        • A Offline
          A Offline
          AcerExtensa
          wrote on 3 May 2012, 07:14 last edited by
          #4

          Try this:
          @
          QNetworkAccessManager *manager = new QNetworkAccessManager();

          connect(manager,SIGNAL(authenticationRequired(QNetworkReply*, QAuthenticator*)),SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*)));
          @

          You know what it will only work for HTTP-Basic authentication? If website has login form, it is mostly POST or GET request needed.

          God is Real unless explicitly declared as Integer.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mark.eastonregenersis.com
            wrote on 3 May 2012, 07:35 last edited by
            #5

            Still getting error message with what you advised to try:
            Object::connect: No such slot MainWindow::provideAuthentication(QNetworkReply*,QAuthenticator*) in mainwindow.cpp
            Object::connect: (receiver name: 'MainWindow')

            I have declared this is the mainwindow.h file under "public slots:"

            Actually, it doesn't have to "automatically" log into the website, even if I can just get it to display the login form so I can manually enter the username and password?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              miroslav
              wrote on 23 May 2012, 14:24 last edited by
              #6

              You have a problem with the signal/slot code, not with QNetworkManager. Have a look at http://doc.qt.nokia.com/4.7-snapshot/signalsandslots.html . Before this debug message is resolved, there is no use in continuing.

              Mirko Boehm | mirko@kde.org | KDE e.V.
              FSFE Fellow
              Qt Certified Specialist

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dreevespracticeinsight.net
                wrote on 21 Jun 2012, 18:49 last edited by
                #7

                This will work:
                @
                QWebView *view = new QWebView();
                QUrl URL="https://website";
                URL.setUserName("username");
                URL.setPassword("password");
                view->load(URL);
                view->show();
                @

                [Edit: Wrapped code in @-tags -- mlong]

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mark.eastonregenersis.com
                  wrote on 28 Nov 2012, 10:51 last edited by
                  #8

                  I am revisiting this problem after several months of doing another project.

                  I tried the https log on above but it still didn't work. Actually, you can't even log in to the website through a normal browser by using the https:// address.

                  It is a pitty that the website doesn't support puting the username and password into the address field (i.e http://username:password@webaddress.com) because that would solve my problems!

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mark.eastonregenersis.com
                    wrote on 29 Nov 2012, 12:49 last edited by
                    #9

                    [quote author="AcerExtensa" date="1336029275"]Try this: @ QNetworkAccessManager manager = new QNetworkAccessManager(); connect(manager,SIGNAL(authenticationRequired(QNetworkReply, QAuthenticator*)),SLOT(provideAuthentication(QNetworkReply*,QAuthenticator*))); @ You know what it will only work for HTTP-Basic authentication? If website has login form, it is mostly POST or GET request needed.[/quote]

                    Once I got the slots working, this way worked!

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      gelignite
                      wrote on 12 Dec 2012, 17:01 last edited by
                      #10

                      [quote author="mark.easton" date="1335906244"]Hi,

                      [...]

                      My code:
                      @
                      QWebview *m_pWebView;
                      m_pWebView->url().setUserName("username");
                      m_pWebView->url().setPassword("password");
                      m_pWebView->load(QUrl("website"));
                      m_pWebView->show();
                      @
                      [/quote]

                      Err, this cannot work. You set username and password on the view's URL just to load a fresh QUrl object which has no username and password set.

                      Just to be clear: setUserName and setPassword belong to QUrl but not to QWebView, hence the values you specify in line 2 and 3 are set on the currently loaded QUrl object. As soon as you load another QUrl object (line 5) the old one is thrown away and username & password of the new QUrl object are used, which might not be set or are set up with incorrect values.

                      Setup url first, then load in your view. There you go:
                      @QUrl url( "website" );
                      url.setUserInfo( "username:password" ); // combined url.setUserName() and url.setPassword()

                      QWebview *m_pWebView = new QWebView();
                      m_pWebView->load( url );
                      m_pWebView->show();@

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mark.eastonregenersis.com
                        wrote on 13 Dec 2012, 08:20 last edited by
                        #11

                        Good stuff. Yeah this way would work for websites that require authentication through the web url.

                        In my case, it was a form that pops up that you have to manually enter the username and password (does not work by puting the username:password in address bar).

                        I had to use QNetworkManager with provideAuthenticationSlot to get my way to work.

                        1 Reply Last reply
                        0
                        • R Offline
                          R Offline
                          rakanor
                          wrote on 8 Aug 2013, 22:09 last edited by
                          #12

                          [quote author="mark.easton" date="1355386852"]Good stuff. Yeah this way would work for websites that require authentication through the web url.

                          In my case, it was a form that pops up that you have to manually enter the username and password (does not work by puting the username:password in address bar).

                          I had to use QNetworkManager with provideAuthenticationSlot to get my way to work.[/quote]

                          Thanks Mark,
                          But you don't necessary need to declare a new QNetworkAcessManager object. You can use the one related to QWebView (QWebView::page()->networkAccessManager())

                          E.g.

                          @connect(QWebView::page()->networkAccessManager(), SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),
                          this, SLOT(handleAuthenticationRequired(QNetworkReply*,QAuthenticator*)));

                          void handleAuthenticationRequired(QNetworkReply*, QAuthenticator* authenticator) {
                          authenticator->setUser("username");
                          authenticator->setPassword("password");
                          }
                          @
                          I tested it, this works fine.

                          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