Facebook Graph API使用哪个Django库?
我目前正在Django中开发一个应用程序,并试图实现Facebook身份验证和对Graph API的请求。我已经看到了一些不同的库,但是执行以下操作的最佳方法是什么:Facebook Graph API使用哪个Django库?,django,facebook,facebook-graph-api,Django,Facebook,Facebook Graph Api,我目前正在Django中开发一个应用程序,并试图实现Facebook身份验证和对Graph API的请求。我已经看到了一些不同的库,但是执行以下操作的最佳方法是什么: 通过Facebook进行用户登录 Django为他们创建一个新用户,并添加他们的uid和oauth令牌 然后,我可以使用Facebook的Python SDK调用Graph API 我确实看到了。普通的Django有这么简单吗?我推荐。很快就启动了我基于Django的Facebook应用程序并开始运行。它包括一个中间件,允许您在视
user_id = request.facebook.graph().get_object("me")['id']
对于oauth令牌:
user_token = request.facebook.auth_token
然后,您可以根据需要将上述内容添加到您的用户模型中。要进行图形API调用,您仍然可以使用fb.py的中间件——无需使用原始的python sdk。上面的用户id代码是图形API调用的完美示例。你可以用fb.py做更多的事情。下载内容包括一个示例django项目,让您开始使用。在我工作的地方,我们做了很多Facebook应用程序开发,因此我们开发了一个非常简单的应用程序
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)
我的公司已经建立了一个库,使将Facebook集成到你的Django应用程序变得非常简单(我们可能已经用这个库构建了10-20个应用程序,包括一些流量巨大的应用程序,所以它已经过战斗测试) 在您的设置中,为您的
FACEBOOK\u密钥
,FACEBOOK\u秘密
,FACEBOOK\u范围
,FACEBOOK\u重定向\u URL
,以及主要用户\u模型
添加值。您还需要将ecl\u facebook.backends.facebookauthbend
添加到您的AUTHENTICATION\u后端。例如,在settings.py中:
在views.py中添加一些视图以处理预验证和后验证逻辑
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
现在只需将这些URL连接到URLs.py文件中,就完成了
# ...
urlpatterns = patterns('app.views',
# ...
url(r'^oauth/facebook/begin$', 'oauth_facebook_begin'),
url(r'^oauth/facebook/complete$', 'oauth_facebook_complete'),
)
希望这有帮助
另外,您可以阅读其余的文档。该用户/repo似乎已消失。是的。试试看——我认为是同一个项目
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
# ...
urlpatterns = patterns('app.views',
# ...
url(r'^oauth/facebook/begin$', 'oauth_facebook_begin'),
url(r'^oauth/facebook/complete$', 'oauth_facebook_complete'),
)