Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha
Accepted Solution

How to set Basic Authentication

Hi all,

 

Would appreciate if anyone can help me explaining How can we set Basic Authentication over HTTP request for Blackberry 10 Cascades.

Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: How to set Basic Authentication (401:Host requires Authentication)

[ Edited ]

I am making Http Request to a URl, which requires authentication.

 

http://pdmobiledev.tmaresources.com/PersonifyDubServiceHostDEMOQA/PersonifyMobileSvc.svc/Directory?o...

 

 

I have tried makinh Http Connection, code is : 

 

void TwitterRequest::getData()
{
	QNetworkAccessManager* netManager = new QNetworkAccessManager();
	if (!netManager)
	{
		qDebug() << "Unable to create QNetworkAccessManager!";
		emit complete("Unable to create QNetworkAccessManager!", false);
		return;
	}

	QString queryUri = "http://pdmobiledev.tmaresources.com/PersonifyDubServiceHostDEMOQA/PersonifyMobileSvc.svc/Directory?o...";
	QUrl url(queryUri);
	QNetworkRequest req(url);

	QNetworkReply* ipReply = netManager->get(req);
	connect(ipReply, SIGNAL(finished()), this, SLOT(onTimelineReply()));
}

void TwitterRequest::onTimelineReply()
{
	QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
	QString response;
	bool success = false;
	if (reply)
	{
		if (reply->error() == QNetworkReply::NoError)
		{
			int available = reply->bytesAvailable();
			if (available > 0)
			{
				int bufSize = sizeof(char) * available + sizeof(char);
				QByteArray buffer(bufSize, 0);
				int read = reply->read(buffer.data(), available);
				response = QString(buffer);
				success = true;
			}
		}
		else
		{
			response =  QString("Error: ") + reply->errorString() + QString(" status:") + reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toString();
			qDebug() << response;
		}
		reply->deleteLater();
	}
	emit complete(response, success);
}

 

This is returning me, while trying to connect "Error: Host requires authentication status:401"

 

So, I need to set basic authentication, for fetching that data.Please tell me how to do that..

 

Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.
Developer
kanaksony
Posts: 571
Registered: ‎04-11-2009
My Device: BB 10 Dev Alpha

Re: How to set Basic Authentication (401:Host requires Authentication)

I have got the solution. I am sharing solution for all those facing similar issue, You can add Authentication Headers as --

 

QNetworkRequest req(url);

// HTTP Basic authentication header value: base64(username:password)
QString concatenated = "username:password";
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
req.setRawHeader("Authorization", headerData.toLocal8Bit());

QNetworkReply* ipReply = netManager->get(req);
Regards,
Kanak Sony
------------------------------------------------------------------------------------------
http://dodevelopnshare.wordpress.com/ | http://www.linkedin.com/profile/view?id=188131481&trk=tab_pro
Please use plain text.