May 1, 2012

mark.easton mark.easton
Lab Rat
10 posts

[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:

  1. QWebview *m_pWebView;
  2. m_pWebView->url().setUserName("username");
  3. m_pWebView->url().setPassword("password");
  4. m_pWebView->load(QUrl("website"));
  5. m_pWebView->show();

10 replies

May 2, 2012

Tobias Hunger Tobias Hunger
Mad Scientist
3125 posts

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.

May 2, 2012

mark.easton mark.easton
Lab Rat
10 posts

What about QNetworkAccessManager?

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

  1. connect(manager,SIGNAL(authenticationRequired(QNetworkReply reply*, QAuthenticator auth*)),SLOT(provideAuthentication(QNetworkReply *reply,QAuthenticator *auth)));

  1. void MainWindow::provideAuthentication(QNetworkReply *reply, QAuthenticator *auth)
  2. {
  3. auth->setUser("username");
  4. auth->setPassword("password");
  5. }

May 3, 2012

AcerExtensa AcerExtensa
Robot Herder
567 posts

Try this:

  1.  
  2. 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.

 Signature 

God is Real unless explicitly declared as Integer.

May 3, 2012

mark.easton mark.easton
Lab Rat
10 posts

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?

May 23, 2012

miroslav miroslav
Ant Farmer
228 posts

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.

 Signature 

Mirko Boehm | .(JavaScript must be enabled to view this email address) | KDE e.V.
FSFE Fellow
Qt Certified Specialist

June 21, 2012

David David
Lab Rat
2 posts

This will work:

  1. QWebView *view = new QWebView();
  2. QUrl URL="https://website";
  3. URL.setUserName("username");
  4. URL.setPassword("password");
  5. view->load(URL);
  6. view->show();

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

November 28, 2012

mark.easton mark.easton
Lab Rat
10 posts

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!

November 29, 2012

mark.easton mark.easton
Lab Rat
10 posts
AcerExtensa wrote:
Try this:
  1.  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.

Once I got the slots working, this way worked!

December 12, 2012

gelignite gelignite
Lab Rat
1 posts

mark.easton wrote:
Hi,

[…]

My code:

  1. QWebview *m_pWebView;
  2. m_pWebView->url().setUserName("username");
  3. m_pWebView->url().setPassword("password");
  4. m_pWebView->load(QUrl("website"));
  5. m_pWebView->show();

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:

  1. QUrl url( "website" );
  2. url.setUserInfo( "username:password" ); // combined url.setUserName() and url.setPassword()
  3.  
  4. QWebview *m_pWebView = new QWebView();
  5. m_pWebView->load( url );
  6. m_pWebView->show();

December 13, 2012

mark.easton mark.easton
Lab Rat
10 posts

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.

 
  ‹‹ object with embed tag doesn’t work properly in 4.8      Very very poor scrolling using QtWebKit ››

You must log in to post a reply. Not a member yet? Register here!