2011-04-04 193 views
4

我已經實現了一個具有用戶登錄/註冊頁面的Django應用程序。我想讓facebook用戶名也可以登錄到我的應用程序中。這樣做,我跟着這個鏈接http://developers.facebook.com/docs/guides/web/#login。藉助該鏈接,我現在可以使用Facebook ID登錄。點擊Facebook登錄按鈕,彈出與Facebook登錄頁面;給用戶名n密碼,使它登錄與Facebook。但登錄後,該頁面將重定向回我的主頁。我需要將其重定向到其他網址,例如http://localhost/login/。有人可以幫我解決這個問題嗎?我將粘貼用於django登錄的html部分。Facebook的登錄Django和URL重定向?

HTML

<body> 
    <div id="fb-root"></div> 
     <script src="http://connect.facebook.net/en_US/all.js"></script> 
     <script> 
     FB.init({ 
      appId:'114322105313139', cookie:true, 
      status:true, xfbml:true 
     }); 
     </script> 


    <fb:login-button>Login with Facebook</fb:login-button> 

</body> 

settings.py

# Django settings for universityDB project. 

DEBUG = True 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
    # ('Your Name', '[email protected]'), 
) 

MANAGERS = ADMINS 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'student',      # Or path to database file if using sqlite3. 
     'USER': 'root',      # Not used with sqlite3. 
     'PASSWORD': 'qburst',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    } 
} 


EMAIL_HOST = "smtp.gmail.com" 
EMAIL_PORT = '587' 
EMAIL_HOST_USER = "[email protected]" #create a gmail id 
EMAIL_HOST_PASSWORD = "xxxx"#pwd for that id 
EMAIL_USE_TLS = True 


FACEBOOK_CACHE_TIMEOUT=1800 
FACEBOOK_APP_ID='xxx' 
FACEBOOK_API_KEY='xxx' 
FACEBOOK_SECRET_KEY='xxxx' 
FACEBOOK_INTERNAL = 'TRUE' 


# Local time zone for this installation. Choices can be found here: 
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 
# although not all choices may be available on all operating systems. 
# On Unix systems, a value of None will cause Django to use the same 
# timezone as the operating system. 
# If running in a Windows environment this must be set to the same as your 
# system time zone. 
TIME_ZONE = 'America/Chicago' 

# Language code for this installation. All choices can be found here: 
# http://www.i18nguy.com/unicode/language-identifiers.html 
LANGUAGE_CODE = 'en-us' 

SITE_ID = 1 

# If you set this to False, Django will make some optimizations so as not 
# to load the internationalization machinery. 
USE_I18N = True 

# If you set this to False, Django will not format dates, numbers and 
# calendars according to the current locale 
USE_L10N = True 

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/" 
MEDIA_ROOT = '' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash if there is a path component (optional in other cases). 
# Examples: "http://media.lawrence.com", "http://example.com/media/" 
MEDIA_URL = '' 

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a 
# trailing slash. 
# Examples: "http://foo.com/media/", "/media/". 
ADMIN_MEDIA_PREFIX = '/media/' 

# Make this unique, and don't share it with anybody. 
SECRET_KEY = 'b_3=te)1b57mqsz^))jg95i%umw=*pug_i*[email protected](ie=8x$' 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.middleware.csrf.CsrfResponseMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'facebook.djangofb.FacebookMiddleware', 
    'facebookconnect.middleware.FacebookConnectMiddleware', 
    # 'facebook.djangofb.FacebookMiddleware', 
    # 'socialregistration.middleware.FacebookMiddleware' ,  

) 


AUTHENTICATION_BACKENDS = (
    'facebookconnect.models.FacebookBackend', 
    'social_auth.backends.facebook.FacebookBackend', 
    'django.contrib.auth.backends.ModelBackend', 
) 



LOGIN_URL   = '/login-form/' 
LOGIN_REDIRECT_URL = '/logged-in/' 
LOGIN_ERROR_URL = '/login-error/' 

SOCIAL_AUTH_ERROR_KEY = 'social_errors' 

SOCIAL_AUTH_COMPLETE_URL_NAME = 'complete' 
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'associate_complete' 

SOCIAL_AUTH_DEFAULT_USERNAME = 'new_social_auth_user' 

SOCIAL_AUTH_EXTRA_DATA = False 

SOCIAL_AUTH_EXPIRATION = 'expires' 

SOCIAL_AUTH_SESSION_EXPIRATION = False 


DUMMY_FACEBOOK_INFO = { 
    'uid':0, 
    'name':'(Private)', 
    'first_name':'(Private)', 
    'pic_square_with_logo':'http://www.facebook.com/pics/t_silhouette.gif', 
    'affiliations':None, 
    'status':None, 
    'proxied_email':None, 
} 

ROOT_URLCONF = 'universityDB.urls' 

TEMPLATE_DIRS = (
    "/home/Desktop/universityDB/templates"    
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'universityDetails', 
    # Uncomment the next line to enable the admin: 
    'django.contrib.admin', 
    'captcha', 
    'facebookconnect', 
    'social_auth', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
) 

回答

3

該代碼看起來就像FB的HTML。如果你需要更多的幫助,你需要給我們一個關於你的Django Auth結構的更好的概念。

還有更多的步驟在你前面。如果我是你,我會考慮Django-SocialAuth,以獲得輕鬆集成的體驗。

編輯: 作爲後續行動納入你的Settings.py的,假如您已經閱讀Facebook Authentication文檔還有SocialAuth README,你將需要建立適當的控制器FB的認證回調URL 。

之後,掛鉤這些SocialAuth意見到當前的認證過程:

def facebook_login(request): 
    """ 
    Facebook login page 
    """ 
    if request.REQUEST.get("device"): 
     device = request.REQUEST.get("device") 
    else: 
     device = "user-agent" 

    params = {} 
    params["client_id"] = FACEBOOK_APP_ID 
    params["redirect_uri"] = request.build_absolute_uri(reverse("socialauth_facebook_login_done")) 

    url = "https://graph.facebook.com/oauth/authorize?"+urllib.urlencode(params) 

    return HttpResponseRedirect(url) 

def facebook_login_done(request): 
    user = authenticate(request=request) 

    if not user: 
     request.COOKIES.pop(FACEBOOK_API_KEY + '_session_key', None) 
     request.COOKIES.pop(FACEBOOK_API_KEY + '_user', None) 

     # TODO: maybe the project has its own login page? 
     logging.debug("SOCIALAUTH: Couldn't authenticate user with Django, redirecting to Login page") 
     return HttpResponseRedirect(reverse('socialauth_login_page')) 

    login(request, user) 

    logging.debug("SOCIALAUTH: Successfully logged in with Facebook!") 

    if request.GET.get('next'): 
     return HttpResponseRedirect(request.GET.get('next')) 
    else: 
     return HttpResponseRedirect(LOGIN_REDIRECT_URL) 
+0

我已經加了我settings.py也。你可以檢查一下嗎? – 2011-04-04 07:03:50

+0

你也可以分享settings.py嗎? – Siddhu 2012-07-31 08:43:43