Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic authentication with Qt (QNetworkAccessManager)

I was trying to perform basic authentication for Twitter from my Qt app. I use QNetworkAccessManager. But I couldn't find any help on this.

But I found a program called qsoapmanager which passes credentials in base64 through the header. Maybe I can do this with QNAM by setting header in QNetowrkRequest. But I failed to find a way.

In qsoapman source, header is set like this:

QHttpRequestHeader header;

header.setValue( "Authorization", QString( "Basic " ).append( auth.data() ) );

Can I do just that with QNAM/QNReq or is there a better way?

like image 498
chanux Avatar asked Oct 03 '09 05:10

chanux


2 Answers

But if you want to do it by just setting the header value, here's how you can do that:

// HTTP Basic authentication header value: base64(username:password)
QString concatenated = username + ":" + password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
request.setRawHeader("Authorization", headerData.toLocal8Bit());
like image 74
Riussi Avatar answered Oct 13 '22 17:10

Riussi


The recommended way is to connect to the authenticationRequired signal and set the credentials from there.

like image 37
Lukáš Lalinský Avatar answered Oct 13 '22 19:10

Lukáš Lalinský