How to setup Facebook Oauth

When we create an Application, we should also consider to let people using their Facebook account for Sign-in purpose. In that case, we need to setup Facebook Oauth.

First you have to create developer account in Facebook.
Go to for registration.
Once you have an account, then you can create a New App.

Pick the name you like for “Display Name”
You also need to specific your email address, and Category.


App ID and App Secret will be created for this application


Then you need to add platform which you want your application to be used. In my case, I want to use facebook oauth for web site only. I select “Website”


For www option, there is a field for the URL. Just set it point to localhost host at this moment. Port is 8000 which it is django default TCP port


We have everything setup at the facebook side.
Now, we need to install django-rest-framework-social-oauth2 in the local host
For more information and detail about django-rest-framework. Here is the URL:

(foodtasker) tiger:foodtasker xxxxx$ pip install django-rest-framework-social-oauth2==1.0.5

Add django-rest-framework-social-oauth2 in the requirement.txt which we previously created for Heroku. So, we can tell Heroku what applications we need to install at Heroku


In order to use django-rest-framework-social-oauth2 in Django, we need to modify the Below is the list of the things we have to add there. Otherwise, it wouldn’t work


        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [


# Facebook configuration

# Define SOCIAL_AUTH_FACEBOOK_SCOPE to get extra permissions from facebook. Email is not sent by default, to get it, you must request the email permission:
    'fields': 'id, name, email'

Once all the things have been added, we can run the migrate commands again to refresh the change we just did

(foodtasker) tiger:foodtasker xxxxx$ python migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, foodtaskerapp, oauth2_provider, sessions, social_django
Running migrations:
  Rendering model states... DONE
  Applying oauth2_provider.0001_initial... OK
  Applying oauth2_provider.0002_08_updates... OK
  Applying oauth2_provider.0003_auto_20160316_1503... OK
  Applying oauth2_provider.0004_auto_20160525_1623... OK
  Applying social_django.0001_initial... OK
  Applying social_django.0002_add_related_name... OK
  Applying social_django.0003_alter_email_max_length... OK
  Applying social_django.0004_auto_20160423_0400... OK
  Applying social_django.0005_auto_20160727_2333... OK
  Applying social_django.0006_partial... OK
(foodtasker) tiger:foodtasker xxxxx$ 

If you run the django development server again, and check the admin page.
You will see there are a few additional things have been added:

  • Django Oauth Toolkit
  • Social_Django


Select Application from “DJANGO_OAUTH_TOOLKIT” and add application. Fill in the form according to the pictures below. Client id and Client secret will be automatically generated by the system. No need to change it





About whoismikechan

I am just a guy who has a family, working daily with my daily work, but always dream about to have my own startup company.
This entry was posted in facebook, Heroku, Python. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s