Question

I'm working with a webservice which requires a valid username/password. From PyQt, I'm accessing the webservice using QNetworkAccessManager which emits the

authenticationRequired (QNetworkReply*, QAuthenticator*)

signal when (obviously), authentication is required. When I fill in the user and psswd for QAuthenticator, everything works fine. However, I can't see how to break the loop when the user is not valid.

From the docs for authenticationRequired:

"If it rejects the credentials, this signal will be emitted again."

For invalid credentials this signal is emitted again, and again, and again... Looking at the error code in the reply showed 0. How is this loop supposed to be broken or handled so that it terminates with an error?

Was it helpful?

Solution

Yeh, it's odd. What I've done previously is check if I've already authenticated with those details and if I have then call QNetworkReply.abort()

OTHER TIPS

What about trying:

QNetworkReply::abort()

or

QNetworkReply::close()

?

Calling QNetworkReply::abort() will cause the request to fail with the error 'Operation Aborted' instead of the original 401 error. The correct thing to do now seems to be not to call QAuthenticator::setUser() or QAuthenticator::setPassword() inside your authenticationRequired() slot. This will leave the QAuthenticatorPrivate::phase as Done which will cause the request to terminate cleanly with the correct error code.

The Qt documentation is rather unclear on this point.

This did not seem to be the behaviour in Qt 4.7, and was introduced at some point in Qt 4.8.

If you return from a slot connected to &QNetworkAccessManager::proxyAuthenticationRequired without calling any QAuthenticator method, the request will be aborted with real HTTP code (e.g. 401 or 407).

For instance you can call QAuthenticator::setUser and setPassword when the user clics OK and call nothing when he clics Cancel. That's all :-)

This is also true for slots connected to &QNetworkAccessManager::authenticationRequired (where you do not have to call QNetworkReply::abort(), you can simply call no QAuthenticator method at all, which preserve HTTP code too, whereas calling abort() alter it).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top