views:

247

answers:

2

EDIT: I added a bounty, if someone could help me figure out what I am doing wrong, its all yours.

Also, I don't really care how this gets done. If there is a library that can help out, or something of that sort that would be great.

Since there is no Captcha involved, I should theoretically be able to log into Flickr and add a contact through code similar to the following...correct?

When I run this code and many many variants of it when I get to the place where I am supposed to submit a form to verify that I am adding a contact, it asks me to log in. Is the only thing this could be my failure to collect the right cookie?

I have IEHTTPHeader for IE to look at whats actually happening and I am trying my best to emulate it. I just can't find where I am going wrong unless I just don't understand something.

Flickr API can't do this

All I want to do is login and add a predefined contact.

        //This Kicks off the process
        private void button2_Click(object sender, EventArgs e)
        {
            string user = "adampeditto"; //Some Random Guy
            CookieContainer cookies = new CookieContainer();
            cookies = LoginYahoo(cookies, user);
            cookies = GetCookies(cookies, user);
            cookies = ClickFlickrButton(cookies, user);
        }

        private CookieContainer ClickFlickrButton(CookieContainer cookies, string user)
        {
            try
            {
                string appURL = "http://www.flickr.com/people/" + user + "/relationship";
                string strPostData = "magic_cookie=a9dd9cd9fd23081f35b8bad2c0ba1878&done=1";

                // Setup the http request.
                HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest;
                wrWebRequest.Method = "POST";
                wrWebRequest.Timeout = 10000;
                wrWebRequest.ContentLength = strPostData.Length;
                wrWebRequest.ContentType = "application/x-www-form-urlencoded";
                CookieContainer cookieContainer = cookies;
                wrWebRequest.CookieContainer = cookieContainer;

                // Post to the login form.
                StreamWriter swRequestWriter = new
                StreamWriter(wrWebRequest.GetRequestStream());
                swRequestWriter.Write(strPostData);
                swRequestWriter.Close();

                // Get the response.
                HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

                // Read the response
                StreamReader srResponseReader = new StreamReader(hwrWebResponse.GetResponseStream());
                string strResponseData = srResponseReader.ReadToEnd();
                srResponseReader.Close();

                return cookieContainer;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private CookieContainer LoginYahoo(CookieContainer cookies, string user)
        {
            string loginName = "YourLoginName";
            string password = "YourPassword";
            string appURL = "https://login.yahoo.com/config/login?.src=flickr&.pc=5134&.scrumb=0&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fpeople%25252F" + user + "%25252Frelationship%25252F&rl=1";
            string strPostData = ".tries=1&.src=flickr&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=0delt5h5l4df0&.v=0&.challenge=3DZF0DFFqdE0m.9MWnCq6LjUZ9gV&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&.done=https%3A%2F%2Flogin.yahoo.com%2Fconfig%2Fvalidate%3F.src%3Dflickr%26.pc%3D5134%26.scrumb%3D0%26.pd%3Dc%253DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26.intl%3Dus%26.done%3Dhttp%253A%252F%252Fwww.flickr.com%252Fsignin%252Fyahoo%252F%253Fredir%253D%25252Fpeople%25252F" + user + "%25252Frelationship%25252F&.pd=flickr_ver%3D0%26c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-%26ivt%3D%26sg%3D&login=" + loginName + "&passwd=" + password + "&.persistent=y&.save=Sign+In";

            // Setup the http request.
            HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as

            HttpWebRequest;
            wrWebRequest.AllowAutoRedirect = true;
            wrWebRequest.Method = "POST";
            wrWebRequest.ContentLength = strPostData.Length;
            wrWebRequest.ContentType = "application/x-www-form-urlencoded";
            CookieContainer cookieContainer = cookies;
            wrWebRequest.CookieContainer = cookieContainer;

            // Post to the login form.
            StreamWriter swRequestWriter = new
            StreamWriter(wrWebRequest.GetRequestStream());
            swRequestWriter.Write(strPostData);
            swRequestWriter.Close();

            // Get the response.
            HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

            // Read the response
            StreamReader srResponseReader = new
            StreamReader(hwrWebResponse.GetResponseStream());
            string strResponseData = srResponseReader.ReadToEnd();
            srResponseReader.Close();

            //YOU ARE NOW LOGGED IN TO YAHOO!
            ShowInBrowser(strResponseData);
            return cookieContainer;
        }

        //These are the places it sends you upon autoredirect after your run LoginYahoo(), if you set AutoRedirect to true there should be no need for this...
        private CookieContainer GetCookies(CookieContainer cookies, string user)
        {
            CookieContainer cookieContainer = cookies;

            string appURL = "http://login.yahoo.com/config/validate?.src=flickr&.pc=5134&.scrumb=6l14Ni2Pz3j&.pd=c%3DE0.GahOp2e4MjkX.5l2HgAoLkpmyPvccpVM-&.intl=us&.done=http%3A%2F%2Fwww.flickr.com%2Fsignin%2Fyahoo%2F%3Fredir%3D%252Fpeople%252F" + user + "%252Frelationship%252F";
            HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest.CookieContainer = cookieContainer;
            wrWebRequest.AllowAutoRedirect = false;
            HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();

            appURL = "http://flickr.com/signin/yahoo/?redir=%2Fpeople%2F" + user + "%2Frelationship%";
            HttpWebRequest wrWebRequest2 = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest2.CookieContainer = cookieContainer;
            wrWebRequest2.AllowAutoRedirect = false;
            HttpWebResponse hwrWebResponse2 = (HttpWebResponse)wrWebRequest2.GetResponse();

            appURL = "http://flickr.com/cookie_check.gne?pass=%2Fpeople%2F" + user + "%2Frelationship%2F&fail=register_cookies.gne";
            HttpWebRequest wrWebRequest3 = WebRequest.Create(appURL) as HttpWebRequest;
            wrWebRequest3.CookieContainer = cookieContainer;
            wrWebRequest3.AllowAutoRedirect = true;
            HttpWebResponse hwrWebResponse3 = (HttpWebResponse)wrWebRequest3.GetResponse();

            return cookieContainer;
        }
+2  A: 

Instead of doing screen scraping and making your life of programmer hard, you could use the Flickr API. You might also find this blog post helpful.

Darin Dimitrov
+1 Do things the right way.
Audrius
Can't. Flickr API doesn't support adding new users.
Blankasaurus
This is all flickr API supports. *flickr.contacts.getList *flickr.contacts.getListRecentlyUploaded *flickr.contacts.getPublicList. Thanks for the blog link though, nice to have on file for other uses. =D
Blankasaurus
It isn't allowed too. Redirect them to flickr and let them register.
Dykam
+1  A: 

I've done something similar with perl's WWW:Mechanize library, look into that.

jaws
Funny you say that. This is what I ended up doing. Worked like a charm.
Blankasaurus