<?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>Paul Donnelly &#187; oauth</title>
	<atom:link href="http://paul.donnelly.org/tag/oauth/feed/" rel="self" type="application/rss+xml" />
	<link>http://paul.donnelly.org</link>
	<description>my personal blog and netstream</description>
	<lastBuildDate>Fri, 26 Mar 2010 00:00:42 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>2-legged OAuth Javascript Function for YQL</title>
		<link>http://paul.donnelly.org/2008/10/31/2-legged-oauth-javascript-function-for-yql/</link>
		<comments>http://paul.donnelly.org/2008/10/31/2-legged-oauth-javascript-function-for-yql/#comments</comments>
		<pubDate>Fri, 31 Oct 2008 07:52:46 +0000</pubDate>
		<dc:creator>Paul Donnelly</dc:creator>
				<category><![CDATA[yahoo]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[yql]]></category>

		<guid isPermaLink="false">http://paul.donnelly.org/?p=36</guid>
		<description><![CDATA[Here is a 2-legged OAuth Javascript function that makes it easy to get YQL results into your Javascript application.
First we want to include the oauth javascript libraries obtained from http://oauth.googlecode.com.

&#60;script type=&#34;text/javascript&#34; src=&#34;http://oauth.googlecode.com/svn/code/javascript/oauth.js&#34;&#62;&#60;/script&#62;
&#60;script type=&#34;text/javascript&#34; src=&#34;http://oauth.googlecode.com/svn/code/javascript/sha1.js&#34;&#62;&#60;/script&#62;

It&#8217;s probably best to download those files to your local server, instead of calling them directly from googlecode in this example.
We then [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a 2-legged OAuth Javascript function that makes it easy to get <a href="http://developer.yahoo.com/yql/console/" target="_blank">YQL</a> results into your Javascript application.</p>
<p>First we want to include the oauth javascript libraries obtained from <a href="http://oauth.googlecode.com" target="_blank">http://oauth.googlecode.com</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://oauth.googlecode.com/svn/code/javascript/oauth.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://oauth.googlecode.com/svn/code/javascript/sha1.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>It&#8217;s probably best to download those files to your local server, instead of calling them directly from googlecode in this example.</p>
<p>We then want to use function makeSignedRequest to make the signed URL string to query.yahooapis.com so we can get the YQL results.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> makeSignedRequest <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>ck<span style="color: #339933;">,</span>cks<span style="color: #339933;">,</span>encodedurl<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>     
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> accessor <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> consumerSecret<span style="color: #339933;">:</span> cks<span style="color: #339933;">,</span> tokenSecret<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>          
	<span style="color: #003366; font-weight: bold;">var</span> message <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> action<span style="color: #339933;">:</span> encodedurl<span style="color: #339933;">,</span> method<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;oauth_version&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;1.0&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;oauth_consumer_key&quot;</span><span style="color: #339933;">,</span>ck<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
	OAuth.<span style="color: #660066;">setTimestampAndNonce</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	OAuth.<span style="color: #660066;">SignatureMethod</span>.<span style="color: #660066;">sign</span><span style="color: #009900;">&#40;</span>message<span style="color: #339933;">,</span> accessor<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> parameterMap <span style="color: #339933;">=</span> OAuth.<span style="color: #660066;">getParameterMap</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">var</span> baseStr <span style="color: #339933;">=</span> OAuth.<span style="color: #660066;">decodeForm</span><span style="color: #009900;">&#40;</span>OAuth.<span style="color: #660066;">SignatureMethod</span>.<span style="color: #660066;">getBaseString</span><span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           
	<span style="color: #003366; font-weight: bold;">var</span> theSig <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parameterMap.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #000066; font-weight: bold;">item</span> <span style="color: #000066; font-weight: bold;">in</span> parameterMap.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> subitem <span style="color: #000066; font-weight: bold;">in</span> parameterMap.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>parameterMap.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span>subitem<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;oauth_signature&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					theSig <span style="color: #339933;">=</span> parameterMap.<span style="color: #660066;">parameters</span><span style="color: #009900;">&#91;</span><span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>                    
					<span style="color: #000066; font-weight: bold;">break</span><span style="color: #339933;">;</span>                      
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> paramList <span style="color: #339933;">=</span> baseStr<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	paramList.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;oauth_signature=&quot;</span><span style="color: #339933;">+</span>theSig<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	paramList.<span style="color: #660066;">sort</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span>b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> b<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> b<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span>  <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> b<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> locString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #000066; font-weight: bold;">in</span> paramList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		locString <span style="color: #339933;">+=</span> paramList<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #339933;">;</span>                
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> finalStr <span style="color: #339933;">=</span> baseStr<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;?&quot;</span> <span style="color: #339933;">+</span> locString.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>locString.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> finalStr<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Here is an example of how to call the function.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">makeSignedRequest<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;API Key HERE&gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&lt;Shared Secret HERE&gt;&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&lt;YQL URL HERE&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you havn&#8217;t already created your API key and Shared Secret, you can do so by going <a href="https://developer.yahoo.com/dashboard/createKey.html" target="_blank">here</a>. Use the URL from the <a href="http://developer.yahoo.com/yql/console/" target="_blank">YQL console</a>, located in the <a href="http://paul.donnelly.org/blog/wp-content/uploads/2008/10/yqlurl.jpg" target="_blank">textarea</a> to the right of where you enter the query.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> signedURL <span style="color: #339933;">=</span> makeSignedRequest<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;dj0yJmk9Rm1MUU9iWmdNZ2FjJmQ9WVdrOVZWWk9Wa3h5TldFbWNHbzlNVEk0TXpNMk1EYzFPQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1kMg--&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;570e2ef3db460b114e6a0a987709a0f6a90b5ec0&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;http://query.yahooapis.com/v1/yql?q=select%20*%20from%20search.news%20where%20query%3D%22obama%22&amp;format=json&amp;callback=myCallback&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>And it&#8217;s easy as that! You then can make cool apps that use <a href="http://www.hunlock.com/blogs/Mastering_JSON_(_JavaScript_Object_Notation_)#quickIDX11" target="_blank">dynamic script nodes</a> to bring in the YQL data.</p>
<p>Here is a <a href="http://paul.donnelly.org/demos/oauth.htm" target="_blank">sample app</a> created with all the ingredients from above. </p>
<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://paul.donnelly.org/2008/10/31/2-legged-oauth-javascript-function-for-yql/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>
