<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PlanetB &#187; Code</title>
	<atom:link href="http://www.planetb.ca/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.planetb.ca</link>
	<description></description>
	<lastBuildDate>Sat, 31 Dec 2011 23:31:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Twitter oAuth in .Net without Web Login</title>
		<link>http://www.planetb.ca/2010/07/twitter-oauth-in-net-without-web-login/</link>
		<comments>http://www.planetb.ca/2010/07/twitter-oauth-in-net-without-web-login/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 02:07:57 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[oAuth]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://www.planetb.ca/?p=278</guid>
		<description><![CDATA[I was interested in getting a Twitter App together to orchestrate some follows and unfollows and user searches for a personal project. In the past, I&#8217;ve used Twitter&#8217;s Basic Authentication API to get my Twitter feed, but according to the Twitter API documentation, Basic Auth is going to be unsupported in August of this year. [...]]]></description>
			<content:encoded><![CDATA[<p>I was interested in getting a <strong>Twitter App</strong> together to orchestrate some follows and unfollows and user searches for a personal project.  In the past, I&#8217;ve used <strong>Twitter&#8217;s Basic Authentication</strong> API to get my Twitter feed, but according to the Twitter API documentation, Basic Auth is going to be unsupported in August of this year.  So I figured I might as well get my feet wet with <strong>Twitter oAuth</strong> authentication.</p>
<p>I came across <a href="http://www.voiceoftech.com/swhitley/?p=856" target="_blank">this article</a> about how to use Twitter oAuth with .Net (via Twitter API documentation) and used the extended oAuth code for my own project.</p>
<p>Unfortunately in all examples of desktop apps that I can find, they all involve opening up Twitter.com in a web browser control to get the user&#8217;s PIN for the app.  Of course, this makes sense, but I wanted to short circuit this process.  I wanted to be able to enter a userid and password and go straight into my account.</p>
<p>Here&#8217;s a quick snip of my code in case you&#8217;re interested in doing the same.  The code uses HTTPWebRequest and HTTPWebResponse to automate the PIN request procedure so a web browser control is not needed.</p>
<p>This code uses Twitter oAuth for .Net from <a href="http://www.voiceoftech.com/swhitley/?p=856">here</a>, which itself extends .Net oAuth from <a href="http://oauth.net/code/">here</a>. The code is probably not complete as I simply took the code snippets from a larger project and as such, some variables may not be declared or initialized properly.</p>
<div style="font-size:8pt;">
<pre class="brush: c#">
//Assumes these vars are populated before below code.
private string _consumerKey = &quot;&quot;;
private string _consumerSecret = &quot;&quot;;
private string _userid = &quot;&quot;;
private string _password = &quot;&quot;;

//These get populated by below code
private string _token=&quot;&quot;;
private Uri AuthUri = null;
private oAuthTwitter oAuth;
private string _pin = &quot;&quot;;

public void authenticate()
{
    oAuth = new oAuthTwitter();
    oAuth.ConsumerKey = _consumerKey;
    oAuth.ConsumerSecret = _consumerSecret;

    Uri AuthUri = new Uri(oAuth.AuthorizationLinkGet());
    _token = HttpUtility.ParseQueryString(AuthUri.Query)[&quot;oauth_token&quot;];
    CookieContainer cookies = null;
    string content = getContent(AuthUri.ToString(), null, &quot;&quot;, null, ref cookies);
    string authenticity_token = Regex.Match(content, &quot;twttr\\.form_authenticity_token = &#039;([^&#039;]+)&#039;;&quot;, RegexOptions.IgnoreCase).Groups[1].Value;

    string post = ue(&quot;authenticity_token&quot;, authenticity_token) +
                    &quot;&amp;&quot; + ue(&quot;oauth_token&quot;, _token) +
                    &quot;&amp;&quot; + ue(&quot;session[username_or_email]&quot;, _userid) +
                    &quot;&amp;&quot; + ue(&quot;session[password]&quot;, _password) + &quot;&amp;Allow=allow&quot;;

    content = getContent(oAuthTwitter.AUTHORIZE, post, &quot;https://www.twitter.com/&quot;, cookies, ref cookies);
    Match m = Regex.Match(content, &quot;oauth_pin\\\&quot;&gt;\\s*([0-9]+)&quot;, RegexOptions.IgnoreCase);
    _pin = &quot;&quot;;
    if (m != null)
    {
        _pin = m.Groups[1].Value;
    }

    oAuth.Token = _token;
    oAuth.AccessTokenGet(_token, _pin);
    if (oAuth.TokenSecret.Length &gt; 0)
    {
        string xml = oAuth.oAuthWebRequest(oAuthTwitter.Method.GET, &quot;http://twitter.com/account/verify_credentials.xml&quot;, String.Empty);
    }
}
private string ue(string parm, string val)
{
    return HttpUtility.UrlEncode(parm) + &quot;=&quot; + HttpUtility.UrlEncode(val);
}

public static string getContent(string url, string requestBody, string referrer, CookieContainer cookies, ref CookieContainer retCookies)
{
    ServicePointManager.CertificatePolicy = new trustedCertificatePolicy();
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Method = (requestBody == null) ? &quot;GET&quot; : &quot;POST&quot;;
    request.UserAgent = &quot;Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)&quot;;
    request.Accept = &quot;text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8&quot;;
    request.Referer = referrer;
    request.CookieContainer = cookies;

    if (requestBody != null)
    {
        request.ContentType = &quot;application/x-www-form-urlencoded&quot;;
        request.ContentLength = System.Text.Encoding.UTF8.GetByteCount(requestBody);
        Stream requestStream = request.GetRequestStream();
        StreamWriter writer = new StreamWriter(requestStream);
        writer.Write(requestBody);
        writer.Flush();
    }
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Stream stream = response.GetResponseStream();
    StreamReader reader = new StreamReader(stream);
    string content = reader.ReadToEnd();

    CookieContainer rc = new CookieContainer();

    UriBuilder ubuilder = new UriBuilder();
    Uri u = new Uri(&quot;https://www.twitter.com&quot;);

    rc.SetCookies(u, response.Headers[&quot;Set-Cookie&quot;]);
    rc.Add(response.Cookies);
    retCookies = rc;

    return content;
}
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.planetb.ca/2010/07/twitter-oauth-in-net-without-web-login/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Syntax Highlight Code in Word Documents</title>
		<link>http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/</link>
		<comments>http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/#comments</comments>
		<pubDate>Sun, 16 Nov 2008 04:28:37 +0000</pubDate>
		<dc:creator>Jamie</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[syntax highlight]]></category>

		<guid isPermaLink="false">http://www.planetb.ca/?p=15</guid>
		<description><![CDATA[Placed a form around Google&#8217;s SyntaxHighlighter javascript code. Made it easier to use.  To get some nice syntax highlighted code into a Word document, use IE and copy and paste some code into the form below and click the button.  A new window will popup with the syntax highlighed code.  Copy all and paste into [...]]]></description>
			<content:encoded><![CDATA[<p>Placed a form around Google&#8217;s <a href="http://code.google.com/p/syntaxhighlighter/">SyntaxHighlighter javascript code.</a> Made it easier to use.  </p>
<p>To get some nice syntax highlighted code into a Word document, use IE and copy and paste some code into the form below and click the button.  A new window will popup with the syntax highlighed code.  Copy all and paste into your document.  Unfortunately Firefox doesn&#8217;t copy and paste into Word like IE does, so you&#8217;re stuck with IE if you&#8217;re looking to copy the resulting formatted code into a Word doc.</p>
<p><center><iframe src="/projects/syntaxHighlighter/" style="background-color:white;color:black;width:550px;height:300px;border:0px;"></iframe></center><br />
<center><a href="http://planetb42.devsecrets.hop.clickbank.net/"><img src="http://appdevsecrets.com/images/nuts/336z280.gif" alt="" width="336" height="280"/></a></center></p>
]]></content:encoded>
			<wfw:commentRss>http://www.planetb.ca/2008/11/syntax-highlight-code-in-word-documents/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>

