[SOLVED] QWebview - website requires authentication



  • 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();
    @


  • Moderators

    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.



  • 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");
    }@



  • 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.



  • 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?



  • 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.



  • 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]



  • 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!



  • [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!



  • [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();@



  • 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 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.