FourSquare Check-in With CSharp

Visual Studio

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:

C#
  static void Checkin(string venueid, double latitude, double longitude)
    {
      string result = HttpPost("http://api.foursquare.com/v1/checkin", "vid=" + venueid + "&private=0&geolat=" + Scatter(latitude) + "&geolong=" + Scatter(longitude), "myuser", "mypassword");
      XmlDocument xml = new XmlDocument();
      xml.LoadXml(result);
      Console.WriteLine(xml.SelectSingleNode("/checkin/message").InnerText);
      try
      {
        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;
      try
      { // 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);
      }
      finally
      {
        if (requestStream != null)
        {
          requestStream.Close();
        }
      }
      try
      { // 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;
    }
C#

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.

Leave a Reply

Your email address will not be published. Required fields are marked *