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 http://developers.facebook.com 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.

screen-shot-2017-02-19-at-6-16-40-pm

App ID and App Secret will be created for this application

screen-shot-2017-02-19-at-6-20-59-pm

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”

screen-shot-2017-02-19-at-6-21-38-pm

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

screen-shot-2017-02-19-at-6-22-08-pm

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:

https://github.com/PhilipGarnero/django-rest-framework-social-oauth2

(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

django-rest-framework-social-oauth2==1.0.5

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

INSTALLED_APPS = [
    'oauth2_provider',
    'social_django',
    'rest_framework_social_oauth2',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                ::::
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]

AUTHENTICATION_BACKENDS = (
    'social_core.backends.facebook.FacebookOAuth2',
    'rest_framework_social_oauth2.backends.DjangoOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)

# Facebook configuration
SOCIAL_AUTH_FACEBOOK_KEY = 'xxxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxxxx'

# 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:
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
    '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 manage.py 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

screen-shot-2017-02-19-at-6-43-20-pm

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

screen-shot-2017-02-19-at-6-45-32-pm

 

 

Advertisements

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:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s