Geeks With Blogs

Lance's TextBox

Here’s how to automatically check-in to FourSquare from C#.  Yes, I did cheat with this for about a week.  Apparently people were wondering how I was checkin in and defending my mayorships while I was out of the country.  Haha.  Anyway, here you go:

    static void Checkin(string venueid, double latitude, double longitude)
      string result = HttpPost("", "vid=" + venueid + "&private=0&geolat=" + Scatter(latitude) + "&geolong=" + Scatter(longitude), "myuser", "mypassword");
      XmlDocument xml = new XmlDocument();
        Console.WriteLine("Current mayor: " + xml.SelectSingleNode("/checkin/mayor/user/firstname").InnerText);
      catch { };
    static string Scatter(double coord)
      Random rnd = new Random(Convert.ToInt32(coord));
      string num = rnd.NextDouble().ToString().Substring(2);
      if (num.Length > 4) num = num.Substring(0, 4);
      string retval = coord.ToString() + num;
      return retval;
    static string HttpPost(string uri, string parameters, string username, string password)
      HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
      webRequest.PreAuthenticate = true;
      webRequest.UserAgent = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C10 Safari/419.3";
      string authstring = username + ":" + password;
      byte[] authbytes = new byte[authstring.Length];
      authbytes = System.Text.Encoding.UTF8.GetBytes(authstring);
      webRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(authbytes));
      webRequest.ContentType = "application/x-www-form-urlencoded";
      webRequest.Method = "POST";
      byte[] bytes = Encoding.ASCII.GetBytes(parameters);
      Stream requestStream = null;
      { // send the Post
        webRequest.ContentLength = bytes.Length;
        requestStream = webRequest.GetRequestStream();
        requestStream.Write(bytes, 0, bytes.Length);
      catch (WebException ex)
        Console.WriteLine("HttpPost: Request error: " + ex.Message);
        if (requestStream != null)
      { // get the response
        WebResponse webResponse = webRequest.GetResponse();
        if (webResponse == null)
        { return null; }
        StreamReader sr = new StreamReader(webResponse.GetResponseStream());
        return sr.ReadToEnd().Trim();
      catch (WebException ex)
        Console.WriteLine("HttpPost: Response error: " + ex.Message);
      return null;

The Checkin function takes the venueid of the place at which you’d like to check-in.  It also takes a latitude and longitude for your current position - just use two decimal places in the longitude and latitude coordinates that you pass in, and the Scatter function will “scatter” them so that you aren’t checking in from the exact same spot every time.

Technorati Tags: ,,,
Posted on Monday, September 27, 2010 12:39 PM | Back to top

Comments on this post: FourSquare Checkin with CSharp

# re: FourSquare Checkin with CSharp
Requesting Gravatar...
Thanks for providing such useful information.I would like to thank you for the efforts you made in writing this post. I am hoping the same best work from you in the future as well.
Left by estimation software on Dec 03, 2010 6:46 AM

Your comment:
 (will show your gravatar)

Copyright © Lance Robinson | Powered by: