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
Contributor
ios_developer
Posts: 21
Registered: ‎05-28-2013
My Device: Z10
My Carrier: Telus
Accepted Solution

QNetworkRequest Basic Authorization

Hi,

 

Am trying to do REST call with Basic authorization

 

	// Creates the network request and sets the destination URL.
	QNetworkRequest request = QNetworkRequest(QUrl(strURL));
	request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
	request.setRawHeader("Accept", "application/vnd.cpc.track+xml");
	request.setRawHeader("Accept-language", "en-CA");
	QString credential = QString(strCredential);
	QByteArray data = credential.toLocal8Bit().toBase64();
	QString authHeader = "Basic " + data;
	qDebug() << "Sending credential : " << authHeader;
	request.setRawHeader("Authorization", authHeader.toLocal8Bit());

 

I tried doing that, but receive an Unknown error. Then when I use Rest client to test the web call, it works, even when I tried to use .NET code, it works too, i tried even sending the .NET B64 encoded string in BB10, still doesn't work

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method;

                // Set Basic Authentication Header using username and password variables
                string auth = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(username + ":" + password));
                request.Headers = new WebHeaderCollection();
                request.Headers.Add("Authorization", auth);
                request.Headers.Add("Accept-Language", "en-CA");

 

 

Please use plain text.
Developer
Benecore
Posts: 400
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: T-Mobile

Re: QNetworkRequest Basic Authorization

[ Edited ]

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

 

Example: (not tested, but i hope it will works)

QNetworkAccessManager *manager = new QNetworkAccessManager();
connect(manager,SIGNAL(authenticationRequired(QNetworkReply*,QAuthenticator*)),SLOT(provideAuthenication(QNetworkReply*,QAuthenticator*)));
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finished(QNetworkReply*)));

QNetworkRequest req(QUrl("http://blabla"));
manager->get(req);

void provideAuthenication(QNetworkReply *reply, QAuthenticator *ator)
{
    qDebug() << reply->readAll(); // this is just to see what we received
    ator->setUser(QString("USERNAME"));
    ator->setPassword(QString("MYPASSWORD"));
}

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Please use plain text.
Contributor
ios_developer
Posts: 21
Registered: ‎05-28-2013
My Device: Z10
My Carrier: Telus

Re: QNetworkRequest Basic Authorization

Yep, tried that, no go

 

void ApplicationUI::onAuthenticationRequired(QNetworkReply* reply, QAuthenticator* authenticated)
{
	qDebug() << reply->readAll();
	QString strUsername = QString("xxxxxxxxxxxx").toAscii().toBase64();
	QString strPassword = QString("xxxxxxxxxxxxxx").toAscii().toBase64();
	authenticated->setUser(strUsername);
	authenticated->setPassword(strPassword);

}

 All I am getting is on qDebug()

 

"" 

"" 

"" 

"" 

"" 

"" 

"" 

"" 

""

 

I can't believe it's so difficult to do basic http authentication in cascades, objective C, c#, java can do it so easily

Please use plain text.
Developer
Benecore
Posts: 400
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: T-Mobile

Re: QNetworkRequest Basic Authorization

Well, i don't have where to test :smileysad:



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Please use plain text.
Developer
kylefowler
Posts: 526
Registered: ‎05-17-2009
My Device: 9900
My Carrier: ATT

Re: QNetworkRequest Basic Authorization

At that point you probably wont have anything in reply. You also should not base64 encode the strings that you pass to setUser and setPassword.
Like all of my posts
Please use plain text.
Developer
Benecore
Posts: 400
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: T-Mobile

Re: QNetworkRequest Basic Authorization

[ Edited ]

Yes, it's unnecessary, but i think still doesn't work to ios_developer, or?

EDIT

Ok, i tested my example ios_developer here 

http://test.webdav.org/auth-basic/

and works flawlessly :Clap: . Are you sure that you have no mistake in the code?



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Please use plain text.
BlackBerry Development Advisor (Retired)
garydev
Posts: 83
Registered: ‎10-10-2012
My Device: Z30
My Carrier: Bell

Re: QNetworkRequest Basic Authorization

Here is an example ( but without the slot ) :

 

https://github.com/blackberry/Cascades-Community-Samples/blob/master/UrbanAirshipClient/src/UrbanAir...

 

Let me know if that helps or not.

Please use plain text.