3

Making a project login with facebook. But it's showing error

my localhost url -- 127.0.0.1:8000/demo/index/

My code is like this

setting.py

INSTALLED_APPS = [

    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social_django',
    'dashboard'
]


MIDDLEWARE = [

    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'social_django.middleware.SocialAuthExceptionMiddleware',
]


TEMPLATES = [

    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.join(BASE_DIR), 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends',  # <--
                'social_django.context_processors.login_redirect',
            ],
        },
    },
]


AUTHENTICATION_BACKENDS = (

    'social_core.backends.facebook.FacebookOAuth2',
    'social_core.backends.google.GoogleOAuth2',
    'django.contrib.auth.backends.ModelBackend',
)


STATIC_URL = '/static/'

LOGIN_URL = '/demo/login/'

LOGIN_REDIRECT_URL = '/'

SOCIAL_AUTH_FACEBOOK_KEY = '184354188769721'
SOCIAL_AUTH_FACEBOOK_SECRET = 'c4d9ed712a59be2f9bb25b5368432f61'

LOGIN_URL = '/demo/login/'

LOGIN_REDIRECT_URL = '/'

url.py

url(r'^facebook/', include('social_django.urls', namespace='social')),

index.html

<a href="{% url 'social:begin' 'facebook' %}">Login with Facebook</a>

I created facebook API and set

domain - empty

site url - empty

in facebook login(plugins)

Valid OAuth redirect URIs - http://localhost:8000/_auth/facebook

Give solution wy its showing this error while login.

URL Blocked: This redirect failed because the redirect URI is not whitelisted in the app’s Client OAuth Settings. Make sure Client and Web OAuth Login are on and add all your app domains as Valid OAuth Redirect URIs.

gawi
  • 2,843
  • 4
  • 29
  • 44

2 Answers2

0

You have to set APP DOMAINS in Facebook APP settings

  1. Go to your App's settings page in http://developers.facebook.com

  2. Click on the dropdown arrow on the top left (next to the name of your app) and click "Create Test App" and give it a name

  3. In the Settings > Basic of that new Test App set the App Domains as "localhost"

check this link for more info - Facebook App: localhost no longer works as app domain

For Oauth URL Forward - try putting only domain name

Vaibhav
  • 1,154
  • 10
  • 26
  • I created test app . still i am getting this error, " Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings" can you tell me that i have to add both test App and main app - APP ID AND SECRET KEYS to setting.py or only main App ID AND KEYS – Purnima Kumari Jul 27 '17 at 06:08
  • did you register domain, Under APP DOMAIN type localhost – Vaibhav Jul 27 '17 at 06:11
  • Ya I did that . I added domain in both app main and test app as localhost – Purnima Kumari Jul 27 '17 at 06:14
  • try with port number localhost:8000 – Vaibhav Jul 27 '17 at 06:15
  • Its automatically taking localhost while i am writing localhost:8000 – Purnima Kumari Jul 27 '17 at 06:23
  • check if while calling FB Api you are getting http or https redirect, localhost will not work with https – Vaibhav Jul 27 '17 at 06:31
  • I am aplied this http://localhost:8000/ and this with my path http://localhost:8000/demo/index. both not working – Purnima Kumari Jul 27 '17 at 06:54
0

I got the answer... I have changed the domain name(27.0.0.1:8000 www.example.com) first using this command..

sudo -i gedit /etc/hosts

and then edited in setting.py

ALLOWED_HOSTS = ["www.example.com"]

Go to your App's settings page in http://developers.facebook.com

add site url http://www.example.com:8000/

and domain name example.com