<?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>The Usware Blog - Django Web Development &#187; facebook</title>
	<atom:link href="http://uswaretech.com/blog/category/facebook/feed/" rel="self" type="application/rss+xml" />
	<link>http://uswaretech.com/blog</link>
	<description>Building Amazing Webapps</description>
	<lastBuildDate>Tue, 08 Jun 2010 14:59:46 +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>Django-SocialAuth &#8211; Login via twitter, facebook, openid, yahoo, google using a single app.</title>
		<link>http://uswaretech.com/blog/2009/08/django-socialauth-login-via-twitter-facebook-openid-yahoo-google/</link>
		<comments>http://uswaretech.com/blog/2009/08/django-socialauth-login-via-twitter-facebook-openid-yahoo-google/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 09:20:33 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[utilities]]></category>
		<category><![CDATA[web2.0]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=667</guid>
		<description><![CDATA[TL;DR version: Here is an app to allow logging in via twitter, facebook, openid, yahoo, google, which should work transparently with Django authentication system. (@login_required, User and other infrastructure work as expected.) Demo and Code.Longer version follow: We are releasing our new app. Django-Socialauth. This app makes it awfully easy, to allow users to login [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Permanent Link: Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
<li><a href='http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/' rel='bookmark' title='Permanent Link: How to build a Facebook app in Django'>How to build a Facebook app in Django</a></li>
<li><a href='http://uswaretech.com/blog/2008/05/parable-of-the-single-sheep-or-how-google-is-destroying-the-internet-and-nobody-seems-to-know/' rel='bookmark' title='Permanent Link: Parable of the single sheep &#8211; Or How Google is destroying the internet, and nobody seems to know.'>Parable of the single sheep &#8211; Or How Google is destroying the internet, and nobody seems to know.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>TL;DR version: Here is an app to allow logging in via twitter, facebook, openid, yahoo, google, which should work transparently with Django authentication system. (@login_required, User and other infrastructure work as expected.) <a href="http://socialauth.uswaretech.net/">Demo</a> and <a href="http://github.com/uswaretech/Django-Socialauth/tree/master">Code</a>.Longer version follow:</p>

<hr />

<p>We are releasing our new app. <a href="http://github.com/uswaretech/Django-Socialauth/tree/master"><strong>Django-Socialauth</strong></a>. This app makes it awfully easy,
to allow users to login your site using Yahoo/Google/Twitter/Facebook/Openid. A demo is available <a href="http://socialauth.uswaretech.net/">here</a>.</p>

<p>This is released under an <a href="http://www.opensource.org/licenses/attribution.php">Attribution Assurance License</a>. A copy of the same is
provided included with the code.</p>

<p>After installing this app, you can use @login_required on any view and users identified
via any means can access protected content.</p>

<hr />

<p>We provide services to integrate and implement this, for a low price of USD 1600.
Please contact us at <a href="mailto:licenses@uswaretech.com">licenses@uswaretech.com</a> to discuss your exact needs.</p>

<hr />

<p>The README is copied here for convenience.</p>

<h3>What it does.</h3>

<p>Allow logging in via various providers.</p>

<h4>Logging In</h4>

<p>This is a application to enable authentication via various third party sites.
In particular it allows logging in via</p>

<ol>
<li>Twitter</li>
<li>Gmail</li>
<li>Facebook</li>
<li>Yahoo(Essentially openid)</li>
<li>OpenId</li>
</ol>

<p>Libs you need to install</p>

<ol>
<li><a href="http://pypi.python.org/pypi/python-openid/">python-openid</a> (<code>easy_install</code>)</li>
<li>python-yadis (<code>easy_install</code>)</li>
<li><a href="http://oauth.googlecode.com/svn/code/python/oauth/">python-oauth</a>(<code>easy_install</code>)</li>
</ol>

<p>The API Keys are available from</p>

<ul>
<li><a href="http://www.facebook.com/developers/createapp.php">http://www.facebook.com/developers/createapp.php</a></li>
<li><a href="https://developer.yahoo.com/dashboard/createKey.html">https://developer.yahoo.com/dashboard/createKey.html</a></li>
<li><a href="https://www.google.com/accounts/ManageDomains">https://www.google.com/accounts/ManageDomains</a></li>
<li><a href="http://twitter.com/oauth_clients">http://twitter.com/oauth_clients</a></li>
</ul>

<h4>How it works.</h4>

<p>Openid: Users need to provide their openid providers. Talk to the providers and
login.<br />
Yahoo: Yahoo is an openid provider. Talk to Yahoo endpoints. (Endpoint: http://yahoo.com)<br />
Google: Google is a provider. Talk to them. (Endpoint: https://www.google.com/accounts/o8/id)<br />
Facebook: Facebook connect provides authentication framework.<br />
Twitter: We use Twitter Oauth for authentication. In theory, Oauth shouldn&#8217;t be
used for authentication. (It is an autorisation framework, not an authentication one),
In practice it works pretty well. Once you have an access_token, and a name, essentially
authenticated.</p>

<p>References</p>

<ol>
<li><a href="http://socialauth.uswaretech.net/">Demo of app</a></li>
<li><a href="http://github.com/uswaretech/Django-Socialauth/tree/master">Code for app</a></li>
<li><a href="http://openid.net/developers/">http://openid.net/developers/</a></li>
<li><a href="http://developer.yahoo.com/openid/">http://developer.yahoo.com/openid/</a></li>
<li><a href="http://code.google.com/apis/accounts/docs/OpenID.html">http://code.google.com/apis/accounts/docs/OpenID.html</a></li>
<li><a href="http://apiwiki.twitter.com/OAuth-FAQ">http://apiwiki.twitter.com/OAuth-FAQ</a></li>
<li><a href="http://developers.facebook.com/connect.php">http://developers.facebook.com/connect.php</a></li>
</ol>

<h2>Below the hoods</h2>

<ol>
<li>For all providers(except Facebook) there are two urls and views. (start and done)</li>
<li>Start sets up the required tokens, and redirects and hands off to the correct
provider.</li>
<li>Provider handles authentication on their ends, and hands off to Us, providing
authorization tokens.</li>
<li>In done, we check if the user with these details already exists, if yes, we
log them in. Otherwise we create a new user, and log them in.</li>
</ol>

<p>For all of these, we use standard django authenication system, with custom
<code>auth_backends</code>, hence all existing views, and decorators as <code>login_required</code>
will work as expected.</p>

<h2>Urls</h2>

<pre><code>/login/ Login page. Has all the login options  
/openid_login/ AND /openid_login/done/  
/yahoo_login/ AND /yahoo_login/done/  
/gmail_login/ AND /gmail_login/done/  
/twitter_login/ AND /twitter_login/done/  
/facebook_login/done/ We dont have a start url here, as the starting tokens are  
set in a popup, as per FB Connect recommendations.
</code></pre>

<h2>Implementation</h2>

<ol>
<li>Install required libraries.</li>
<li>Get tokens and populate in localsettings.py</li>
<li>Set the token callback urls correctly at Twitter and Facebook.</li>
<li>Add the OpenId middleware. Set the Authentication backends. (Set in localsettings.example.py)</li>
</ol>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Permanent Link: Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
<li><a href='http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/' rel='bookmark' title='Permanent Link: How to build a Facebook app in Django'>How to build a Facebook app in Django</a></li>
<li><a href='http://uswaretech.com/blog/2008/05/parable-of-the-single-sheep-or-how-google-is-destroying-the-internet-and-nobody-seems-to-know/' rel='bookmark' title='Permanent Link: Parable of the single sheep &#8211; Or How Google is destroying the internet, and nobody seems to know.'>Parable of the single sheep &#8211; Or How Google is destroying the internet, and nobody seems to know.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2009/08/django-socialauth-login-via-twitter-facebook-openid-yahoo-google/feed/</wfw:commentRss>
		<slash:comments>144</slash:comments>
		</item>
		<item>
		<title>How to build a Facebook app in Django</title>
		<link>http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/</link>
		<comments>http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 07:35:14 +0000</pubDate>
		<dc:creator>shabda</dc:creator>
				<category><![CDATA[facebook]]></category>
		<category><![CDATA[Application programming interface]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[Facebook Markup Language]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://uswaretech.com/blog/?p=136</guid>
		<description><![CDATA[Facebook has a simple and robust API which allows users to build applications for the Facebook platform. We being the intelligent developers will use the superb Django framework to build our application. The completed code for this is available at https://svn.uswaretech.com/favlang/. The completed application is deployed to facebook here. How does a Facebook application work? [...]


Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/08/django-socialauth-login-via-twitter-facebook-openid-yahoo-google/' rel='bookmark' title='Permanent Link: Django-SocialAuth &#8211; Login via twitter, facebook, openid, yahoo, google using a single app.'>Django-SocialAuth &#8211; Login via twitter, facebook, openid, yahoo, google using a single app.</a></li>
<li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Permanent Link: Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/' rel='bookmark' title='Permanent Link: Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.'>Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p></p><p>Facebook has a simple and robust API which allows users to build applications for the Facebook platform.
We being the intelligent developers will use the superb Django framework to build our application.</p>

<p>The completed code for this is available at <a href="https://svn.uswaretech.com/favlang/">https://svn.uswaretech.com/favlang/</a>. The completed
application is deployed to facebook <a href="http://apps.facebook.com/1673458a9d3ddaa8c6f888d7150da256/">here</a>.</p>

<h2>How does a Facebook application work?</h2>

<p><img src="http://uswaretech.com/blog/wp-content/uploads/2009/02/facebook.jpg" alt="" title="facebook" class="aligncenter size-full wp-image-129" width="478" height="372"></p>

<ol>
<li>The users make a request to apps.facebook.com/yourapp/page1/</li>
<li>The Facebook servers make a request to yourserver.com/page1/</li>
<li>You get the requests and can make calls to Facebook API, and use FQL to query the Facebook tables.</li>
<li>You send back a page written in FBML to FB servers.</li>
<li>FB servers render your FBML to HTML.</li>
<li>FB servers send a HTML request to the user.</li>
</ol>

<h1>What tools does FB provide you?</h1>

<ol>
<li>FQL: Facebook Query Language. This is a language similar to SQL. Sample this</li>
</ol>

<p><code>SELECT name, pic FROM user WHERE uid=211031 OR uid=4801660</code></p>

<ol>
<li><p>Facebook API: This is a set of RESTful urls which can be used to get data from Facebook and
to do actions on the behalf of the logged in users.</p></li>
<li><p>FBML: Facebook Markup Language. This is a set of tags, similar to Html tags, which allow you to
get your page rendered with extra information on the facebook servers.</p></li>
</ol>

<p><code>&lt;fb:name uid="211031"&gt;</code></p>

<p>would show the name of the user with the id.</p>

<h2>What else do you need?</h2>

<ol>
<li>A server, which can run Django. Duh.</li>
<li>We are using Python, so we can use FB rest API directly. But somebody has already done the hard work and written
the awesome Pyfacebook library, which makes talking to Facebook a breeze.</li>
</ol>

<h2>What will we build?</h2>

<p>We will build a application which allows you to store your favorite programming language at Facebook.
This has a single page with single input box where you can store your favorite programming language.</p>

<p>The completed code for this is available at <a href="https://svn.uswaretech.com/favlang/">https://svn.uswaretech.com/favlang/</a>. The completed
application is deployed to facebook <a href="http://apps.facebook.com/1673458a9d3ddaa8c6f888d7150da256/">here</a>.</p>

<h2>Getting Started</h2>

<ol>
<li>Go to http://www.facebook.com/developers/</li>
<li>Create a new app, give it a name.</li>
<li>Get your application api key, and application secret.</li>
<li>Map callback url to the base of your Server.</li>
<li>Download the Pyfacebook library, and put it where Python can find it. The following import statements should work.
<code>import facebook</code> and <code>import facebook.djangofb</code></li>
</ol>

<h2>Setting up your App</h2>

<ol>
<li><p>Edit settings.py and add the following settings.</p>

<p><code>FACEBOOK_API_KEY = '1673458a9d3ddaa8c6f888d7150da256'</code><br />
<code>FACEBOOK_SECRET_KEY = '666197caab406752474bd0c6695a53f6'</code></p></li>
<li><p>Add <code>facebook.djangofb.FacebookMiddleware</code> to <code>MIDDLEWARE_CLASSES</code></p></li>
<li><p>Create an app named Favlang to hold our Code.</p></li>
</ol>

<h1>Our App</h1>

<p>Its a simple Django app with Models, views and Urls. The only difference with a normal Django app is that,
a. The templates are in FBML.
b. We will use FQL and FBAPI to talk to FB.</p>

<h2>Urls.py</h2>

<pre><code>from django.conf.urls.defaults import *

urlpatterns = patterns('favlang.views',
    (r'^$', 'canvas'),
)
</code></pre>

<p>A basic Urlpatter, mapping patterns to views, nothing FB specific to see here.</p>

<h2>Models.py</h2>

<pre><code>from django.db import models

class FacebookUser(models.Model):
    """A simple User model for Facebook users."""

    # We use the user's UID as the primary key in our database.
    id = models.IntegerField(primary_key=True)
    language = models.CharField(max_length=64, default='Python')
</code></pre>

<p>A normal models.py. Nothing FB specific to see here. (Instead of an autoincrementing PK, we have a PK which we will set manually to FB uid.)</p>

<h2>Views.py</h2>

<pre><code>from django.http import HttpResponse
from django.views.generic.simple import direct_to_template

import facebook.djangofb as facebook

from favlang.models import FacebookUser

@facebook.require_login()
def canvas(request):
    # Get the User object for the currently logged in user
    user, created = FacebookUser.objects.get_or_create(id = request.facebook.uid)

    # Check if we were POSTed the user's new language of choice
    if 'language' in request.POST:
        user.language = request.POST['language'][:64]
        user.save()

    # User is guaranteed to be logged in, so pass canvas.fbml
    # an extra 'fbuser' parameter that is the User object for
    # the currently logged in user.
    return direct_to_template(request, 'favlang/canvas.fbml', extra_context={'fbuser': user})
</code></pre>

<p>Ok. SO finally something FB specific. Lets see what is happening behind the scenes.</p>

<ol>
<li><p>We are putting our view behind a <code>@facebook.require_login()</code> decorator. This is similar to the <code>login_required</code>
decorator. It makes sure that a valid FB user is logged in before it allows access to our view.</p></li>
<li><p>The Middleware <code>facebook.djangofb.FacebookMiddleware</code> attaches a <code>facebook</code> object to the request, which provides
access to the logged in user and some other relevant data.</p></li>
<li><p>We created a FacebookUser for the currently logged in user, in our database, with this line.</p></li>
</ol>

<p>user, created = FacebookUser.objects.get_or_create(id = request.facebook.uid)</p>

<ol>
<li>We returned the FBML with our data. The FBML is parsed by the Facebook servers and HTML returned to the user.</li>
</ol>

<h2>The template</h2>

<pre><code>&lt;fb:header&gt;
  {% comment %}
    We can use {{ fbuser }} to get at the current user.
    {{ fbuser.id }} will be the user's UID, and {{ fbuser.language }}
    is his/her favorite language (Python <img src='http://uswaretech.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .
  {% endcomment %}
  Welcome, &lt;fb:name uid="{{ fbuser.id }}" firstnameonly="true" useyou="false"&gt;!
&lt;/fb:name&gt;

&lt;div class="clearfix" style="border: 1px solid rgb(216, 223, 234); padding: 10px; float: left; margin-left: 30px; margin-bottom: 30px; width: 500px;"&gt;
  Your favorite language is {{ fbuser.language }}.
  &lt;br&gt;&lt;br&gt;

  &lt;div class="grayheader clearfix"&gt;
    &lt;br&gt;&lt;br&gt;

    &lt;form action="." method="post"&gt;
      &lt;input name="language" value="{{ fbuser.language }}" type="text"&gt;
      &lt;input value="Change" type="submit"&gt;
    &lt;/form&gt;
  &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>This template is written in FBML. FBML is a superset of HTML. The tags which start with <code>&lt;fb:</code> are=&#8221;" facebook=&#8221;" specific=&#8221;" tags.=&#8221;" for=&#8221;" example=&#8221;" the=&#8221;" tag=&#8221;" fb:name=&#8221;" <code>=""&gt;&lt;fb:name uid="{{ fbuser.id }}" firstnameonly="true" useyou="false"&gt;</code> renders the
name of the user whose uid is passed in uid.</p>

<p>There is also a form which allows user to change their Favorite language. In views.py this is handled as</p>

<pre><code>if 'language' in request.POST:
    user.language = request.POST['language'][:64]
    user.save()
</code></pre>

<p>Resources.</p>

<ol>
<li><a href="http://developers.facebook.com/">Facebook developers</a>.</li>
<li><a href="http://wiki.developers.facebook.com/">Facebook developers wiki</a>.</li>
<li><a href="http://forum.developers.facebook.com/">Facebook developers forum</a>.</li>
<li>[Pyfacebook]</li>
<li><a href="http://wiki.developers.facebook.com/index.php/API">FBAPI Documentation</a></li>
<li><a href="http://wiki.developers.facebook.com/index.php/FQL">FQL Documentation</a></li>
<li><p><a href="http://wiki.developers.facebook.com/index.php/FBML">FBML Documentation</a></p></li>
<li><p><a href="https://svn.uswaretech.com/favlang/"><strong>Code listing</strong></a></p></li>
<li><a href="http://apps.facebook.com/1673458a9d3ddaa8c6f888d7150da256/"><strong>Live Application</strong></a></li>
</ol>

<p></fb:name></fb:`></fb:header></fb:name></p>


<p>Related posts:<ol><li><a href='http://uswaretech.com/blog/2009/08/django-socialauth-login-via-twitter-facebook-openid-yahoo-google/' rel='bookmark' title='Permanent Link: Django-SocialAuth &#8211; Login via twitter, facebook, openid, yahoo, google using a single app.'>Django-SocialAuth &#8211; Login via twitter, facebook, openid, yahoo, google using a single app.</a></li>
<li><a href='http://uswaretech.com/blog/2009/04/develop-twitter-api-application-in-django-and-deploy-on-google-app-engine/' rel='bookmark' title='Permanent Link: Develop Twitter API application in django and deploy on Google App Engine'>Develop Twitter API application in django and deploy on Google App Engine</a></li>
<li><a href='http://uswaretech.com/blog/2010/01/tools-of-pro-django-developer/' rel='bookmark' title='Permanent Link: Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.'>Tools of Pro Django developer &#8211; aka What powers dinette and almost every app we write.</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://uswaretech.com/blog/2009/02/how-to-build-a-facebook-app-in-django/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
