<?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>maSnun&#039;s logs &#187; oauth</title>
	<atom:link href="http://masnun.com/blog/tag/oauth/feed/" rel="self" type="application/rss+xml" />
	<link>http://masnun.com/blog</link>
	<description>Personal Blog of maSnun</description>
	<lastBuildDate>Sat, 24 Jul 2010 04:33:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Fun with Twitter OAuth: The Source Code</title>
		<link>http://masnun.com/blog/2009/08/30/fun-with-twitter-oauth-the-source-code/</link>
		<comments>http://masnun.com/blog/2009/08/30/fun-with-twitter-oauth-the-source-code/#comments</comments>
		<pubDate>Sun, 30 Aug 2009 07:08:21 +0000</pubDate>
		<dc:creator>masnun</dc:creator>
				<category><![CDATA[Blog Post]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://masnun.com/?p=480</guid>
		<description><![CDATA[In my previous post, I mentioned about the twitter OAuth app hosted at: http://masnun.com/twitter-app/ . Here it comes, the source code. I am not going to include the Twitter OAuth Library source. You have to download it from Github. The &#8230; <a href="http://masnun.com/blog/2009/08/30/fun-with-twitter-oauth-the-source-code/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In my previous post, I mentioned about the twitter OAuth app hosted at: <a href="http://masnun.com/twitter-app/">http://masnun.com/twitter-app/</a> . Here it comes, the source code.</p>
<p>I am not going to include the Twitter OAuth Library source. You have to download it from Github. The link is available in my previous post.</p>
<p><strong>consumer.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Filename: consumer.php</span>
<span style="color: #666666; font-style: italic;">// contains the consumer key and consumer secret</span>
<span style="color: #666666; font-style: italic;">// works as a configuration file that can be included in other</span>
<span style="color: #666666; font-style: italic;">// scripts.</span>
<span style="color: #000088;">$consumer_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Ll0Sq8HyJzBOqXqksH7N4w'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$consumer_secret</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'WwMdrN13UTJL79KyI6unm3xTm5tdF8E4U3FMKE7i5co'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>index.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Filename: index.php</span>
<span style="color: #666666; font-style: italic;">// The file that gets called when you visit the app home page</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// require twitterOAuth lib</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'twitterOAuth.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// require the consumer details -- consumer key and consumer secret</span>
<span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'consumer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//construct a new twitter object</span>
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TwitterOAuth<span style="color: #009900;">&#40;</span><span style="color: #000088;">$consumer_key</span><span style="color: #339933;">,</span><span style="color: #000088;">$consumer_secret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// get the token to make an authorization request</span>
<span style="color: #000088;">$token</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// store the token for further use</span>
<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;token&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_token'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;token_secret&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_token_secret'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// build the authorization url</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAuthorizeURL</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_token'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// redirect the user to the authorization url automatically</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: <span style="color: #006699; font-weight: bold;">$url</span> &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><strong>return.php</strong></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//Filename: return.php</span>
<span style="color: #666666; font-style: italic;">// It's the callback file -- gets called</span>
<span style="color: #666666; font-style: italic;">// the user is returned to this file on authorization</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// include twitter OAuth library as usual</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'twitterOAuth.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// include the consumer details</span>
<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'consumer.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// retrieve the tokens</span>
<span style="color: #000088;">$tok</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;token&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tok_sec</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;token_secret&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// construct the twitter object</span>
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TwitterOAuth<span style="color: #009900;">&#40;</span><span style="color: #000088;">$consumer_key</span><span style="color: #339933;">,</span><span style="color: #000088;">$consumer_secret</span><span style="color: #339933;">,</span><span style="color: #000088;">$tok</span><span style="color: #339933;">,</span><span style="color: #000088;">$tok_sec</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retrieve the access token set by the authorization</span>
<span style="color: #000088;">$access_token</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$twitter</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getAccessToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// parse the tokens and seperate them</span>
<span style="color: #000088;">$tok</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$access_token</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_token'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tok_secret</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$access_token</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'oauth_token_secret'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print_r($access_token);</span>
<span style="color: #666666; font-style: italic;">// construct the twitter object</span>
<span style="color: #666666; font-style: italic;">// this time with the access tokens</span>
<span style="color: #000088;">$twitter</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TwitterOAuth<span style="color: #009900;">&#40;</span><span style="color: #000088;">$consumer_key</span><span style="color: #339933;">,</span><span style="color: #000088;">$consumer_secret</span><span style="color: #339933;">,</span><span style="color: #000088;">$tok</span><span style="color: #339933;">,</span><span style="color: #000088;">$tok_secret</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// make API call</span>
<span style="color: #000088;">$req</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$twitter</span> <span style="color: #339933;">-&gt;</span><span style="color: #004000;">OAuthRequest</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://twitter.com/statuses/update.xml'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Check out: http://masnun.com/twitter-app/ :)'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// print_r($req);</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//redirect the user to twitter home page</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Location: http://twitter.com &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Notes On OAuth:</b><br />
First we, construct the twitter object with the consumer details.<br />
Then we ask for a request token. We build the authorization url with this token.<br />
We send the user to the auth URL.<br />
When the user returns, again construct a twitter object with consumer details and the request token we retrieved previously.<br />
Now that the user is authorized, we ask for access tokens.<br />
We have the access tokens now, so build a new twitter object that has full access to the API.</p>
<p>Now, we can make API calls.</p>
<p>The steps may seem a bit odd at first look. Just go through a couple of times and you&#8217;ll get it clearly.</p>
<p>Enjoy OAuthentication, Enjoy Twitter enginnering !!</p>
]]></content:encoded>
			<wfw:commentRss>http://masnun.com/blog/2009/08/30/fun-with-twitter-oauth-the-source-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Twitter OAuth Fun :D</title>
		<link>http://masnun.com/blog/2009/08/29/twitter-oauth-fun-d/</link>
		<comments>http://masnun.com/blog/2009/08/29/twitter-oauth-fun-d/#comments</comments>
		<pubDate>Sat, 29 Aug 2009 00:33:12 +0000</pubDate>
		<dc:creator>masnun</dc:creator>
				<category><![CDATA[Blog Post]]></category>
		<category><![CDATA[humour]]></category>
		<category><![CDATA[oauth]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://masnun.com/?p=467</guid>
		<description><![CDATA[My University is off for about a month and I am thinking of passing this huge time in some sort of productive work. I love coding and experimenting. Twitter and PHP being my favourites, I decided to check out the &#8230; <a href="http://masnun.com/blog/2009/08/29/twitter-oauth-fun-d/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My University is off for about a month and I am thinking of passing this huge time in some sort of productive work. I love coding and experimenting. Twitter and PHP being my favourites, I decided to check out the Twitter API and cook something up with PHP. I have already used the Basic Authentication API calls in the past (to develop the twitter mobile publisher at <a href="http://masnun.com/twitter/">http://masnun.com/twitter/</a> which is my major tool to tweet while I am on the go). This time I decided to try the OAuth API mainly for two reasons &#8212; 1) I need to explore the OAuth system and 2) I have a project coming on based on the twitter OAuth API.</p>
<p>So, I started studying the Twitter OAuth API and learnt it pretty well. Then I thought what kind of application I could build with this very newly gathered knowledge ? Why not make some fun ? Yeah, I just can&#8217;t help making fun every now and then.</p>
<p>Finally, here it is &#8212; <a href="http://masnun.com/twitter-app/">http://masnun.com/twitter-app/</a> <img src='http://masnun.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It&#8217;s a little devil app that automatically takes you to the authorization URL and asks for OAuth based authentication. If you allow it, it will automatically make a tweet using your account. The tweet will be nothing bad though, just a link back to the app <img src='http://masnun.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>To develop this app, I have used the official Twitter OAuth library for PHP written by Abraham. <strong>Download it: </strong>  <a href="http://github.com/abraham/twitteroauth">http://github.com/abraham/twitteroauth</a> .</p>
<p><strong>Points to Ponder:</strong></p>
<ul>
<li>An application can do almost anything to your twitter account once you approve it.</li>
<li>Twitter says, the access token they provide doesn&#8217;t expire. That means once you approve an application, it can use your account forever if they are clever enough to store your access token details.</li>
<li>While approving an application, you never know what kind of permission the application has &#8212; read only or read-write both. I think this option should be left upon the users. The users should be able to choose whether he wants to provide write access or not.</li>
</ul>
<p>I have seen some web sites exploit Twitter accounts. TweeterFollow is a decent example of such nasty practices. Once you provide your credentials, they&#8217;ll regularly tweet their links from your account. So think twice before you approve an application though OAuth is safer than providing password since you can choose to withdraw your permission to an application.</p>
<p>I will write detailed about OAuth and provide the source code in another post. Just now, I am feeling extremely sleepy. Woke up to have my Sehri and didn&#8217;t return to bed. It&#8217;s time I had a nap&#8230; <img src='http://masnun.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://masnun.com/blog/2009/08/29/twitter-oauth-fun-d/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
