문제 설명
Facebook Graph API에 사용할 Django 라이브러리는 무엇입니까? (Which Django library to use for Facebook Graph API?)
저는 현재 Django에서 애플리케이션을 개발 중이며 그래프 API에 대한 Facebook 인증 및 요청을 구현하려고 합니다. 몇 가지 다른 라이브러리를 보았지만 다음을 수행하는 가장 좋은 방법은 무엇입니까?
- 사용자가 Facebook을 통해 로그인하도록 합니다.
- Django는 새로운 라이브러리를 만듭니다. 사용자를 지정하고 uid 및 oauth 토큰을 추가합니다.
- 그런 다음 Facebook의 Python SDK를 사용하여 Graph API를 호출할 수 있습니다.
이 예. 일반 Django에서 그렇게 간단합니까?
참조 솔루션
방법 1:
My company has built a library that makes integrating Facebook into your Django application dead simple (we've probably built 10‑20 apps with the library, including some with huge amounts of traffic, so it's been battle‑tested).
pip install ecl‑facebook==1.2.7
In your settings, add values for your FACEBOOK_KEY
, FACEBOOK_SECRET
, FACEBOOK_SCOPE
, FACEBOOK_REDIRECT_URL
, and PRIMARY_USER_MODEL
. You'll also need to add ecl_facebook.backends.FacebookAuthBackend
to your AUTHENTICATION_BACKENDS
. For example, in settings.py:
# These aren't actual keys, you'll have to replace them with your own :)
FACEBOOK_KEY = "256064624431781"
FACEBOOK_SECRET = "4925935cb93e3446eff851ddaf5fad07"
FACEBOOK_REDIRECT_URL = "http://example.com/oauth/complete"
FACEBOOK_SCOPE = "email"
# The user model where the Facebook credentials will be stored
PRIMARY_USER_MODEL = "app.User"
AUTHENTICATION_BACKENDS = (
# ...
'ecl_facebook.backends.FacebookAuthBackend',
)
Add some views in your views.py to handle pre‑ and post‑authentication logic.
from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect
from ecl_facebook.django_decorators import facebook_begin, facebook_callback
from ecl_facebook import Facebook
from .models import User
# ...
@facebook_begin
def oauth_facebook_begin(request):
# Anything you want to do before sending the user off to Facebook
# for authorization can be done here.
pass
@facebook_callback
def oauth_facebook_complete(request, access_token, error):
if error is None:
facebook = Facebook(token)
fbuser = facebook.me()
user, _ = User.objects.get_or_create(facebook_id=fbuser.id, defaults={
'access_token': access_token})
user = authenticate(id=user.id)
login(request, user)
return HttpResponseRedirect("/")
else:
# Error is of type ecl_facebook.facebook.FacebookError. We pass
# the error back to the callback so that you can handle it
# however you want.
pass
Now just hook up these URLs in your urls.py file and you're done.
# ...
urlpatterns = patterns('app.views',
# ...
url(r'^oauth/facebook/begin$', 'oauth_facebook_begin'),
url(r'^oauth/facebook/complete$', 'oauth_facebook_complete'),
)
Hope this helps!
P.S. You can read the rest of the docs here.
방법 2:
We do a lot of Facebook Application development where I work, and so we've developed an open‑source library that makes everything about it really easy.
from django.http import HttpResponse
from fandjango.decorators import facebook_authorization_required
@facebook_authorization_required
def foo(request, *args, **kwargs):
return HttpResponse("Your name is %s" % request.facebook_user.first_name)
방법 3:
I recommend https://github.com/egnity/fb.py. Got my Django‑based Facebook app up and running in no time. It includes a middleware that allows you to run code like this in your view:
for the user id:
user_id = request.facebook.graph().get_object("me")['id']
for the oauth token:
user_token = request.facebook.auth_token
You can then add the above to your User model as you please. To make Graph API calls, you can still use fb.py's middleware ‑‑ no need for using the primitive python‑sdk. The user_id code above is a perfect example of a Graph API call. There's much more you can do with fb.py. The download includes a sample django project to get you going.
(by Cole Gleason、dwlz、Johannes Gorset、Simon Kagwi)