-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
363 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.contrib import admin | ||
|
||
# Register your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class DjangoPyfbConfig(AppConfig): | ||
name = 'django_pyfb' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from pyfb import Pyfb | ||
from django.http import HttpResponse, HttpResponseRedirect | ||
from django.shortcuts import render_to_response | ||
|
||
from django.conf import settings | ||
|
||
def index(request): | ||
return render_to_response("index.html", {"FACEBOOK_APP_ID": settings.FACEBOOK_APP_ID}) | ||
|
||
|
||
#This view redirects the user to facebook in order to get the code that allows | ||
#pyfb to obtain the access_token in the facebook_login_success view | ||
def facebook_login(request): | ||
|
||
facebook = Pyfb(settings.FACEBOOK_APP_ID) | ||
return HttpResponseRedirect(facebook.get_auth_code_url(redirect_uri=settings.FACEBOOK_REDIRECT_URL)) | ||
|
||
|
||
#This view must be refered in your FACEBOOK_REDIRECT_URL. For example: http://www.mywebsite.com/facebook_login_success/ | ||
def facebook_login_success(request): | ||
|
||
code = request.GET.get('code') | ||
|
||
facebook = Pyfb(settings.FACEBOOK_APP_ID) | ||
facebook.get_access_token(settings.FACEBOOK_SECRET_KEY, code, redirect_uri=settings.FACEBOOK_REDIRECT_URL) | ||
|
||
return _render_user(facebook) | ||
|
||
|
||
|
||
#Login with the js sdk and backend queries with pyfb | ||
def facebook_javascript_login_sucess(request): | ||
|
||
access_token = request.GET.get("access_token") | ||
|
||
facebook = Pyfb(settings.FACEBOOK_APP_ID) | ||
facebook.set_access_token(access_token) | ||
|
||
return _render_user(facebook) | ||
|
||
|
||
def _render_user(facebook): | ||
|
||
me = facebook.get_myself() | ||
|
||
welcome = "Welcome <b>%s</b>. Your Facebook login has been completed successfully!" | ||
return HttpResponse(welcome % me.name) | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
""" | ||
Django settings for djangoapp3 project. | ||
Generated by 'django-admin startproject' using Django 2.0.13. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/2.0/topics/settings/ | ||
For the full list of settings and their values, see | ||
https://docs.djangoproject.com/en/2.0/ref/settings/ | ||
""" | ||
|
||
import os | ||
|
||
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) | ||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
# Quick-start development settings - unsuitable for production | ||
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ | ||
|
||
# SECURITY WARNING: keep the secret key used in production secret! | ||
SECRET_KEY = '_v#-7jls#!h7y)(nd(me2xvf=nncp6b-^)jqpm0)q8r0g=9gbe' | ||
|
||
# SECURITY WARNING: don't run with debug turned on in production! | ||
DEBUG = True | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
|
||
# Application definition | ||
|
||
INSTALLED_APPS = [ | ||
'django.contrib.admin', | ||
'django.contrib.auth', | ||
'django.contrib.contenttypes', | ||
'django.contrib.sessions', | ||
'django.contrib.messages', | ||
'django.contrib.staticfiles', | ||
|
||
'django_pyfb', | ||
] | ||
|
||
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', | ||
] | ||
|
||
ROOT_URLCONF = 'djangoapp3.urls' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [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', | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
|
||
WSGI_APPLICATION = 'djangoapp3.wsgi.application' | ||
|
||
# Database | ||
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases | ||
|
||
DATABASES = { | ||
'default': { | ||
'ENGINE': 'django.db.backends.sqlite3', | ||
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), | ||
} | ||
} | ||
|
||
|
||
# Password validation | ||
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators | ||
|
||
AUTH_PASSWORD_VALIDATORS = [ | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', | ||
}, | ||
{ | ||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', | ||
}, | ||
] | ||
|
||
|
||
# Internationalization | ||
# https://docs.djangoproject.com/en/2.0/topics/i18n/ | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
|
||
TIME_ZONE = 'UTC' | ||
|
||
USE_I18N = True | ||
|
||
USE_L10N = True | ||
|
||
USE_TZ = True | ||
|
||
|
||
# Static files (CSS, JavaScript, Images) | ||
# https://docs.djangoproject.com/en/2.0/howto/static-files/ | ||
|
||
STATIC_URL = '/static/' | ||
|
||
# Facebook related Settings | ||
FACEBOOK_APP_ID = '535394336554651' | ||
FACEBOOK_SECRET_KEY = 'c26350ed86d9d98de5e9e5461e5ac492' | ||
FACEBOOK_REDIRECT_URL = 'http://localhost:8000/facebook_login_success' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""djangoapp3 URL Configuration | ||
The `urlpatterns` list routes URLs to views. For more information please see: | ||
https://docs.djangoproject.com/en/2.0/topics/http/urls/ | ||
Examples: | ||
Function views | ||
1. Add an import: from my_app import views | ||
2. Add a URL to urlpatterns: path('', views.home, name='home') | ||
Class-based views | ||
1. Add an import: from other_app.views import Home | ||
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') | ||
Including another URLconf | ||
1. Import the include() function: from django.urls import include, path | ||
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) | ||
""" | ||
from django.contrib import admin | ||
from django.urls import path | ||
from django_pyfb import views | ||
|
||
urlpatterns = [ | ||
|
||
path(r'', views.index), | ||
path(r'facebook_login', views.facebook_login), | ||
path(r'facebook_login_success', views.facebook_login_success), | ||
path(r'facebook_javascript_login_sucess', views.facebook_javascript_login_sucess), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
WSGI config for djangoapp3 project. | ||
It exposes the WSGI callable as a module-level variable named ``application``. | ||
For more information on this file, see | ||
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ | ||
""" | ||
|
||
import os | ||
|
||
from django.core.wsgi import get_wsgi_application | ||
|
||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoapp3.settings") | ||
|
||
application = get_wsgi_application() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djangoapp3.settings") | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
"available on your PYTHONPATH environment variable? Did you " | ||
"forget to activate a virtual environment?" | ||
) from exc | ||
execute_from_command_line(sys.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<html> | ||
<head> | ||
|
||
</head> | ||
<div id="fb-root"></div> | ||
<script> | ||
|
||
function isConnected(response) { | ||
return response.status == 'connected'; | ||
} | ||
|
||
function getLoginStatus(FB) { | ||
|
||
FB.getLoginStatus(function(response) { | ||
|
||
if (isConnected(response)) { | ||
onLogin(response); | ||
} | ||
else { | ||
FB.login(onLogin); | ||
} | ||
}); | ||
} | ||
|
||
function onLogin(response) { | ||
|
||
if (isConnected(response)) { | ||
location.href = '/facebook_javascript_login_sucess?access_token=' + response.authResponse.accessToken; | ||
} | ||
} | ||
|
||
window.fbAsyncInit = function() { | ||
|
||
FB.init({ | ||
appId : '{{FACEBOOK_APP_ID}}', | ||
channelUrl : 'http://localhost:8000/media/channel.html', | ||
status : true, | ||
cookie : true, | ||
xfbml : true, | ||
oauth : true, | ||
}); | ||
|
||
}; | ||
|
||
(function(d){ | ||
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;} | ||
js = d.createElement('script'); js.id = id; js.async = true; | ||
js.src = "http://connect.facebook.net/en_US/all.js"; | ||
d.getElementsByTagName('head')[0].appendChild(js); | ||
}(document)); | ||
|
||
</script> | ||
|
||
<body> | ||
<button onclick="location.href='/facebook_login'">Facebook Python Login</button><br/><br/> | ||
<button onclick="getLoginStatus(FB)">Facebook Javascript Login</button> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.