Authentication using Facebook

In order to let your users log in with their Facebook account to your site, add the following line to the AUTHENTICATION_BACKENDS setting in the settings.py file of your project:

'social_core.backends.facebook.FacebookOAuth2',

In order to add social authentication with Facebook, you will need a Facebook developer account and to create a new Facebook application. Open https://developers.facebook.com/apps/ in your browser. You will see the following header in the site:

Click on the Add a New App button. You will see the following form to create a new app ID:

Enter Bookmarks as Display Name, add a contact email address, and click on Create App ID. You will see a dashboard for your new app that displays different features you can set up for your app. Look for the following Facebook Login box and click on Set Up:

You will be asked to choose the platform, as follows:

Select the Web platform. You will see the following form:

Enter http://mysite.com:8000/ as your Site URL and click on the Save button. You can skip the rest of the quickstart process. In the left-hand menu, click on DashboardYou will see something similar to the following:

Copy the App ID and App Secret keys and add them to the settings.py file of your project, as follows:

SOCIAL_AUTH_FACEBOOK_KEY = 'XXX' # Facebook App ID
SOCIAL_AUTH_FACEBOOK_SECRET = 'XXX' # Facebook App Secret

Optionally, you can define a SOCIAL_AUTH_FACEBOOK_SCOPE setting with the extra permissions you want to ask Facebook users for:

SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

Now, go back to Facebook and click on Settings. You will see a form with multiple settings for your app. Add mysite.com under App Domains, as follows:

Click on Save Changes. Then, in the left-hand menu, click on Facebook Login. Ensure that only the following settings are active:

  • Client OAuth Login
  • Web OAuth Login
  • Embedded Browser OAuth Login

Enter http://mysite.com:8000/social-auth/complete/facebook/ under Valid OAuth redirect URIs. The selection should look like this:

Open the registration/login.html template of your account application and append the following code at the bottom of the content block:

<div class="social">
<ul>
<li class="facebook"><a href="{% url "social:begin" "facebook" %}">Sign in with Facebook</a></li>
</ul>
</div>

Open http://mysite.com:8000/account/login/ in your browser. Now, the login page will look as follows:

Click on the Sign in with Facebook button. You will be redirected to Facebook, and you will see a modal dialog asking for your permission to let the Bookmarks application access your public Facebook profile:

Click on the Continue as ... button. You will be logged in and redirected to the dashboard page of your site. Remember that we have set this URL in the LOGIN_REDIRECT_URL setting. As you can see, adding social authentication to your site is pretty straightforward.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset