Ibraheem Osama Mohamed (ابراهيم اسامه محمد)

Welcome to my blog :)

JavaScript (Chakra) WinJS and C# cURL authentication Username and Password in Windows 8 Ex Twitter Stream API

leave a comment »

I was asked to make a HTML Windows 8 store app that uses  Twitter stream API that open a connection between your app and twitter. But to access Twitter steam API you must have an account in twitter and you have to sign in every time you call the api.
You can test , copy this link and past it in your browser https://stream.twitter.com/1/statuses/filter.json?track=Egypt.
From here came the problem because I’ll not ask the client to sign in every time he run my application, so the solution was cURL.

cURL : A free client-side URL transfer library, supporting FTP, FTPS, Gopher, HTTP, HTTPS, SCP, SFTP, TFTP, Telnet, DICT, the file URI scheme, LDAP, LDAPS, IMAP, POP3, SMTP and RTSP. The library supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, Kerberos, HTTP form based upload, proxies, cookies, user-plus-password authentication, file transfer resume, and HTTP proxytunneling.

The first thing I thought that WinJS xhr doesn’t contain a username and password but I was wrong 🙂 so I  implemented it C# and call it from Windows Runtime Component and I start.
Here is the C# code

            string track = مصر;

            track = System.Net.WebUtility.UrlEncode(track);//for arabic words

            string url = https://stream.twitter.com/1/statuses/filter.json?track=” + track;

            WebRequest GETRequest = WebRequest.Create(url);

            GETRequest.Method = “GET”;

            string userName = “UserName”;

            string password = “Password”;

            string credentials = userName + “:” + password;

            GETRequest.Credentials = new NetworkCredential(userName, password);

            string line;

            using (var GETResponse = await GETRequest.GetResponseAsync())


                using (var GETResponseStream = GETResponse.GetResponseStream())


                    StreamReader sr = new StreamReader(GETResponseStream);

                    while ((line = sr.ReadLine()) != null)


                        textblk.Text = line;

                        JsonObject JO = JsonObject.Parse(line);




But After I finished the C# part I told my self is this the right thinking ?? I read Winjs.xhr documentation again and I found a username and password authentication. The best way is to use Winjs xhr

var sentObject = encodeURIComponent(مصر);//for arabic words

                WinJS.xhr({ url: https://stream.twitter.com/1/statuses/filter.json?track=”+sentObject , type:                 “GET”,user:“UserName”,password:“Password” }).then(

            function (result) {

                var data = result.responseText;

            }, function (error) {

                var er = error.responseText;

            }, function (progress) {

                var data = progress.responseText;//the result is here not in the result because it is a live stream API


Last I want to remind you of something twitter stream API is a live stream API so every time you get the result you’ll find it in the progress function not the result
Ibraheem Osama Mohamed


Written by Ibraheem Osama Mohamed

October 12, 2012 at 10:34 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: