<?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>Capture the Conversation &#187; iPhone</title>
	<atom:link href="http://www.capturetheconversation.com/tag/iphone/feed" rel="self" type="application/rss+xml" />
	<link>http://www.capturetheconversation.com</link>
	<description>Social Media Conversation</description>
	<lastBuildDate>Mon, 14 May 2012 18:29:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>iPhone, Facebook, oAuth 2.0 and the Graph API. A Tutorial, Part 2.</title>
		<link>http://www.capturetheconversation.com/technology/iphone-facebook-oauth-2-0-and-the-graph-api-a-tutorial-part-2</link>
		<comments>http://www.capturetheconversation.com/technology/iphone-facebook-oauth-2-0-and-the-graph-api-a-tutorial-part-2#comments</comments>
		<pubDate>Tue, 29 Jun 2010 19:50:27 +0000</pubDate>
		<dc:creator>Room214</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Facebook Graph API]]></category>
		<category><![CDATA[iPad]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[oAuth]]></category>
		<category><![CDATA[oAuth 2.0]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.capturetheconversation.com/?p=1273</guid>
		<description><![CDATA[NOTE: I&#8217;ve moved the source code for this project to Git Hub, please get it here:  http://github.com/reallylongaddress/iPhone-Facebook-Graph-API Preface In part 1 of this tutorial we walked through obtaining an oAuth 2.0 access token from Facebook via an iPhone (or any iTouch device).  If you wish to go back and review the oAuth 2.0 process in some detail HERE. In the second part of this tutorial I’ll show you how to leverage my pseudo-API to: Login to Facebook using oAuth 2.0 Request extended permissions for your mobile application (photos, videos, publish stream and offline access) Get your profile data Get your friend list Get your feed Post to your feed Post a photo (via a local image (UIImage) not a url) Get metadata Delete a feed post (via a Post) Get search results Get (and display) the author’s avatar Convention There are 2 APIs being talked about in this tutorial. To...]]></description>
			<content:encoded><![CDATA[<p><strong>NOTE:</strong> I&#8217;ve moved the source code for this project to Git Hub, please get it here:  <a title="FbGraphAPI" href="http://github.com/reallylongaddress/iPhone-Facebook-Graph-API" target="_blank">http://github.com/reallylongaddress/iPhone-Facebook-Graph-API</a></p>
<p><strong>Preface</strong></p>
<p>In part 1 of this tutorial we walked through obtaining an <strong>oAuth 2.0 access token from Facebook via an iPhone</strong> (or any iTouch device).  If you wish to go back and review the oAuth 2.0 process in some detail <a title="iPhone, Facebook, oAuth 2.0 and the Graph API. A Tutorial." href="http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api" target="_blank">HERE</a>.</p>
<div id="attachment_1286" class="wp-caption aligncenter" style="width: 468px"><img class="size-large wp-image-1286" title="combined" src="http://www.capturetheconversation.com/wp-content/uploads/2010/06/combined1-458x431.png" alt="iPhone oAuth Facebook login and extended permissions images" width="458" height="431" /><p class="wp-caption-text">Facebook Login and Extended Permission Dialogs</p></div>
<p>In the second part of this tutorial I’ll show you how to leverage my pseudo-API to:</p>
<ul>
<li>Login to Facebook using oAuth 2.0</li>
<li>Request <strong>extended permissions for your mobile application</strong> (photos, videos, publish stream and offline access)</li>
<li>Get your profile data</li>
<li>Get your friend list</li>
<li>Get your feed</li>
<li>Post to your feed</li>
<li>Post a photo (via a local image (UIImage) not a url)</li>
<li>Get metadata</li>
<li>Delete a feed post (via a Post)</li>
<li>Get search results</li>
<li>Get (and display) the author’s avatar</li>
</ul>
<p><strong> </strong></p>
<p><strong>Convention</strong></p>
<p>There are 2 APIs being talked about in this tutorial. To avoid confusion, I’m going to refer to them consistently as Facebook’s  ‘Graph API’ and my ‘pseudo-API’*.  <a title="Facebook Graph API" href="http://graph.facebook.com" target="_blank">Facebook’s Graph API</a> is the stuff we’re interacting with via HTTP Get and Post calls.  The pseudo-API is the Objective-C/iPhone code that’s facilitating the interaction.</p>
<p><strong> </strong></p>
<p><em>*-I call it a pseudo-API if for no other reason I haven’t proven to myself it supports 100% of the Facebook Graph API functions.</em></p>
<blockquote><p>An aside about my API design philosophy:  I could have written the pseudo-API to be much more robust and feature rich, with exceptions, logging, auto-magical json parsing, full featured functions like ‘getMyWallFeed’, etc.  However with robustness and features comes complexity and dependencies.  I kept everything aside from the absolute core functionality required to read/write data from/to the Facebook Graph API out of this implementation.  In short, I’ve left it primitive enough that anybody should be able to extend/wrap it easily, if they see fit.</p></blockquote>
<p>The pseudo-API has only 3 classes.  The core functionality lies in the FbGraph.m file.  The other two classes support this core class.  FbGraphFile is used when uploading a file to Facebook.  FbGraphResponse is, I would hope, self-explanatory.</p>
<p>We’re going to skip over the steps required to create a Facebook application.  I covered that in Part 1<a href="../technology/iphone-facebook-oauth2-graph-api"></a>:  <a title="iPhone, Facebook, oAuth 2.0 and the Graph API. A Tutorial." href="http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api" target="_blank">iPhone, Facebook, oAuth 2.0 and the Graph API.  A Tutorial</a>.  If you completed part 1 of the tutorial, you can use the same Facebook application without modification here.</p>
<p><strong>Sample Code:</strong></p>
<p><a title="FbGraphAPI" href="http://github.com/reallylongaddress/iPhone-Facebook-Graph-API" target="_blank">http://github.com/reallylongaddress/iPhone-Facebook-Graph-API</a></p>
<p><strong>Creating the FbGraph Object</strong></p>
<p>Before we can interact with the Graph API, we need to make a connection to it. And before we make a connection to it, we need a FbGraph object instance.  We do so like:</p>
<pre>self.fbGraph = [[FbGraph alloc]initWithFbClientID:client_id];</pre>
<p>Where:</p>
<pre>FbGraph *fbGraph;</pre>
<p>And</p>
<pre>NSString *client_id = @"123145257717248";</pre>
<p>The client_id should be YOUR Facebook application id.  I’ve left my application ID in here for no other reason than the tutorial code will work ‘out of the box’.</p>
<p><strong>The login process</strong></p>
<p>Now we have our fbGraph object instance, we’ll want to ask Facebook for a login screen.  Additionally we’ll need to let Facebook know the extended permissions we’re requesting for our app.  Here’s the code to do that:</p>
<pre>[fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access"];</pre>
<p>There are 3 major things to note in this line (found in oAuth2TestViewController).</p>
<p>1)  We’re asking the fbGraph object to initialize the authentication process by calling the function: authenticateUserWithCallbackObject</p>
<p>2)  We’re setting a callback object (self) and a callback function (fbGraphCallback).   This object and function will be called upon completion of the oAuth authentication process.</p>
<p>3)  We’re requesting extended permissions:@&#8221;user_photos,user_videos,publish_stream,offline_access”</p>
<p>When this function is called, the pseudo-API will find the root application window**, stick in a UIWebView, and ask Facebook for a login screen (passing along your client_id and requested extended permissions):</p>
<pre>NSString *url_string = [NSString stringWithFormat:@"https://graph.facebook.com/oauth/authorize?client_id=%@&amp;redirect_uri=%@&amp;scope=%@&amp;type=user_agent&amp;display=touch", facebookClientID, redirectUri, extended_permissions];</pre>
<p><em>**-There’s a second </em><em>authenticateUserWithCallbackObject function that allows you to specify a specific view you wish the login screen to be anchored/rendered in, if you don’t want it to render within the root view of your application.  Look at the FbGraph class for further details.</em><em> </em></p>
<p>Note the ‘<strong>display=touch</strong>’ parameter.  It tells Facebook we’d like a login screen optimized for an iPhone/iPod touch screen.</p>
<p>Now that the process is started the UIWebView will render the login window that Facebook has returned to us.</p>
<p>After you’ve successfully authenticated to Facebook, you will be presented with a second screen with an extended permissions request dialog.  2 things to note here:  First, all permissions are unified into a single step.  Second, after you’ve approved the permissions, you won’t have to complete this step or see this screen again (so long as you don’t revoke the permissions).</p>
<p><strong>Under the Hood of the Authentication Process</strong></p>
<p>There’s some http redirects involved with the oAuth 2.0 (User-Agent flow) login process.  The FbGraph object is a UIWebViewDelegate, one of the functions associated with this delegate class is: webViewDidFinishLoad.  This function is called several times during the authentication process.  When the URL contains “<a href="http://www.facebook.com/connect/login_success.html#access_token=..........">access_token=</a>” we’re golden.  We’ve successfully logged into Facebook.  When the pseudo-API sees this string, it parses out our oAuth access token, stores it to a class level variable, removes the UIWebView we inserted and finally calls the callback function we defined, returning control to your application.</p>
<p><strong>The Rest of the Pseudo-API</strong></p>
<p>The core of the pseudo-API is about 250 lines of code (including whitespace and comments), which is very little indeed.  This is possible because the Facebook Graph API does everything via simple HTTP Gets and Posts.  In fact, once you’ve figured out how to do Get and Post with the Graph API, you’ve pretty much figured out everything.</p>
<p>If you’re familiar with the current Facebook Connect implementation, you’ll immediately notice Graph API is immeasurably less complex and more consistent.</p>
<p>So, I could go through and explain how everything works in painful (and highly repetitive) detail…but I’m going to peace out, let you read over the code, dissect it, add some breakpoints and get your hands dirty.</p>
<p>FYI:  There is very intentionally very little UI in the app. Rather than having you, the reader, have to figure out my UI conventions as well as Interface Builder, I’ve kept it simple and dumped most all output to the debugger console.  The code is simple, the pseudo-API is simple, the Graph API is simple……</p>
<p>I hope you take a look at my pseudo-API and agree, it’ simple…that’s the idea.</p>
<p>If you find this post useful, if you include this code or the concepts you learned here in an app, if you extend this into a more full featured API….I’d love to know!</p>
<p>Happy hacking.</p>
<p><a href="http://www.twitter.com/dominicdimarco">Dominic</a></p>
<p><a href="mailto:ddimarco@room214.com">ddimarco@room214.com</a></p>
<p><a href="http://twitter.com/dominicdimarco">@dominicdimarco</a></p>
<p><strong>Sample Code:</strong></p>
<p><a title="FbGraphAPI" href="http://github.com/reallylongaddress/iPhone-Facebook-Graph-API" target="_blank">http://github.com/reallylongaddress/iPhone-Facebook-Graph-API</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.capturetheconversation.com/technology/iphone-facebook-oauth-2-0-and-the-graph-api-a-tutorial-part-2/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>iPhone, Facebook, oAuth 2.0 and the Graph API.  A Tutorial.</title>
		<link>http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api</link>
		<comments>http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api#comments</comments>
		<pubDate>Tue, 25 May 2010 18:55:06 +0000</pubDate>
		<dc:creator>Room214</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[Android]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.capturetheconversation.com/?p=1019</guid>
		<description><![CDATA[NOTE: The source code for this tutorial is still linked below, however the FULL Facebook/iPhone Graph API can be found here:  http://github.com/reallylongaddress/iPhone-Facebook-Graph-API The new Facebook Graph API looks to be the cat&#8217;s meow, the bee’s knees, the coolest thing since sliced bread. Ok, that&#8217;s a bit much&#8230; but it is a whole lot more powerful, easier and cross-platform consistent than the previous plethora of Facebook APIs.  The Graph API was announced at Facebook&#8217;s F8 conference just over a month ago.  At the time of release, I was surprised no iPhone SDK was made available. I fully expected that at least an unofficial SDK as well as full blown tutorials would be out en-mass by now, but to no avail. Not nearly patient enough to wait for an official SDK or iPhone API from Facebook, I asked Google how to “Facebook oAuth 2.0 iPhone” and was disappointed with the results. It...]]></description>
			<content:encoded><![CDATA[<p><a rel="attachment wp-att-1103" href="http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api/attachment/1"><img class="alignnone size-medium wp-image-1103" title="1" src="http://www.capturetheconversation.com/wp-content/uploads/2010/05/1-300x168.jpg" alt="" width="300" height="168" /></a></p>
<p><strong>NOTE:</strong> The source code for this tutorial is still linked below, however the FULL Facebook/iPhone Graph API can be found here:  <a title="FbGraphAPI" href="http://github.com/reallylongaddress/iPhone-Facebook-Graph-API" target="_blank">http://github.com/reallylongaddress/iPhone-Facebook-Graph-API</a></p>
<hr width='100%'/>
<p>The new <a href="http://graph.facebook.com" target="_blank">Facebook Graph API</a> looks to be the cat&#8217;s meow, the bee’s knees, the coolest thing since sliced bread. Ok, that&#8217;s a bit much&#8230; but it is a whole lot more powerful, easier and cross-platform consistent than the previous plethora of Facebook APIs.  The <a href="http://graph.facebook.com" target="_blank">Graph API </a>was announced at Facebook&#8217;s F8 conference just over a month ago.  At the time of release, I was surprised no <strong>iPhone SDK</strong> was made available. I fully expected that at least an unofficial SDK as well as full blown tutorials would be out en-mass by now, but to no avail.</p>
<p>Not nearly patient enough to wait for an official SDK or iPhone API from Facebook, I asked Google how to <em>“Facebook oAuth 2.0 iPhone”</em> and was disappointed with the results. It turns out, there are very few, woefully incomplete examples of <strong>how to authenticate to Facebook via oAuth 2.0</strong>, from an <strong>Objective-C / native iPhone application</strong>.  The best implementation I found was a <a href="http://github.com/ryanscott/bamboo/">pseudo API</a>, however I don&#8217;t care for it since it uses the old Facebook Connect authentication scheme, then implements the graph API on top of that layer.</p>
<p>Adding insult to injury, there was lots of news last week about the <strong>Android Graph API</strong> but no love for the iPhone:</p>
<blockquote><p>“Facebook’s mobile development team soft launched a Facebook SDK for Android, bringing functionality that was previously only available on the iPhone to the Android platform. It gets better: Facebook gave the Android platform a de facto exclusive on two of its newest initiatives: Open Graph APIs and OAuth 2.0.”</p>
<p>– <a href="http://www.allfacebook.com/2010/05/android-gets-facebook-graph-api-before-the-iphone/"><em>Will M, Allfacebook.com</em></a></p></blockquote>
<p><em><strong>Aside</strong>:  My theory on why there&#8217;s not iPhone/Objective-C API is:  That functionality is going to be directly (and deeply) integrated in the forthcoming iPhone OS 4.0</em> <em>(June?)</em>.</p>
<p>All of that being said, I slashed, burned and figured it out.  Here&#8217;s the result, a very simple end-to-end example of how to connect to Facebook via oAuth 2.0 on the iPhone.</p>
<h3>Part 1: Connecting to Facebook with oAuth 2.0 on the iPhone</h3>
<p>Part 2 of this series will cover how to interact with the Graph  API.</p>
<p><strong>NOTE:</strong> You can go to <a href="http://graph.facebook.com" target="_blank">http://graph.facebook.com</a> to play with the Graph API, directly<strong> in your browser.</strong></p>
<p><strong>Assumptions and requirements:</strong></p>
<ul>
<li><strong><em>Facebook:</em></strong> You&#8217;ve installed the <a href="http://www.facebook.com/developers/" target="_blank">Facebook Developer Application</a>.</li>
<li><strong><em>iPhone:</em></strong> You&#8217;ve installed the iPhone Xcode SDK installed (I believe most any version will work) and have a some level of knowledge about objective-C, Interface Builder, etc.</li>
</ul>
<h4>Facebook Setup</h4>
<ul>
<li>Go to the <a href="http://www.facebook.com/developers">Facebook developer App</a> and install if you haven&#8217;t already</li>
<li>Once you&#8217;ve installed <a href="http://www.facebook.com/developers/createapp.php">Setup a New Application</a></li>
<li>Next you need to configure a few app settings:
<ul>
<li>BASIC tab:  enter an <em>Application Name</em></li>
<li>AUTHENTICATION tab: un-check and leave everything blank</li>
</ul>
</li>
<li>Save changes.  Save yourself some frustration and make note of the <strong>Application ID</strong> now.</li>
</ul>
<p><span style="text-decoration: underline;"><strong>Sample Code:</strong></span></p>
<p>[download id="3" format="1"]</p>
<h4>iPhone Setup</h4>
<p>Open up the Xcode project.  There’s only 2 functions at play here:</p>
<h5>viewDidLoad</h5>
<p>This function is called as soon as the view has completely loaded.  It then asks the UIWebView to begin the oAuth 2.0 authorization process by sending a request to:</p>
<p>https://graph.facebook.com/oauth/authorize?client_id=<strong>your_facebook_app_id</strong>&#038;redirect_uri=http://www.facebook.com/connect/login_success.html&#038;type=user_agent&#038;display=touch</p>
<p>(NOTE:  <em>display=touch</em>, we&#8217;ll get back to this in a bit)</p>
<h5>webViewDidFinishLoad</h5>
<p>This function is called several times throughout the login process execution. These multiple web view finished calls have to do with server redirects within the oAuth 2.0 process flow.  We’re only interested when the requested URL contains:</p>
<pre>access_token=
</pre>
<p>And the associated obj-c code to identify when this occurs is:</p>
<pre>NSRange access_token_range = [url_string rangeOfString:@"access_token="];
if (access_token_range.length &gt; 0) {</pre>
<p>Then, this code extracts the oAuth token out of the URL we received back the Facebook oAuth servers:</p>
<pre>int from_index = access_token_range.location + access_token_range.length;
NSString *access_token = [url_string substringFromIndex:from_index];
</pre>
<h4>iPhone Configuration</h4>
<p>In <strong>oAuth2TestViewController.m</strong> update the client_id variable with your Facebook Application ID:</p>
<pre>/*Facebook Application ID*/
NSString *client_id = @"YOUR_FB_APPLICATION_ID";
</pre>
<h4>Running the App</h4>
<p>Once you’ve pasted your Facebook Application ID into the right place, you should be able to run the application straight away.</p>
<p>When you first launch the simulator <em>oAuth2TestViewController</em> it will automagically initialize the oAuth login procedure via the webView object.  Barring any errors or configuration issues, you should get a Facebook login screen optimized for an iTouch device (NOTE:  thus the <em>display=touch </em>we made note of above).  Finally after you’ve logged in you’ll see your oAuth access token in the Xcode debug console.</p>
<p><span style="text-decoration: underline;"><strong>Sample Code:</strong></span></p>
<p>[download id="3" format="1"]</p>
<p class="pull_1 grid_8 alpha omega"><em>That’s it.  No bells &amp; whistles, no buttons, no Interface Builder, no UITableViewControllers; just the bare minimum required to get an oAuth 2.0 token via an itouch device from Facebook. </em></p>
<p class="pull_1 grid_8 alpha omega">
<p class="pull_1 grid_8 alpha omega"><em>In <strong>part 2</strong>, we’ll interact with the Graph </em><em>GETing from and POSTing data to</em><em> it, using a </em><em>pseudo-API.</em></p>
<p class="pull_1 grid_8 alpha omega"><em>Find me here: </em><em><a href="http://twitter.com/dominicdimarco" target="_blank">@dominicdimarco</a></em></p>
<p class="pull_1 grid_8 alpha omega"><em><strong>NOTE: </strong></em>(5/27/2010 11:09 AM MST) The original post has been updated incorporating feedback from comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

