Facebook Graph API 使用哪个 Django 库?

发布于 2024-10-04 06:11:26 字数 385 浏览 3 评论 0原文

我目前正在 Django 中开发一个应用程序,并尝试实现 Facebook 身份验证和对 Graph API 的请求。我见过一些不同的库,但执行以下操作的最佳方法是什么:

  1. 让用户通过 Facebook 登录。
  2. Django 为他们创建一个新用户并添加他们的 uid 和 oauth 令牌。
  3. 然后我可以使用 Facebook 的 Python SDK 调用 Graph API。

我确实看到了这个示例。普通的 Django 就这么简单吗?

I'm currently developing an application in Django and trying to implement Facebook authentication and requests to the Graph API. I've seen a few different libraries out there, but what is the best way to do the following:

  1. Have a user login via Facebook.
  2. Django creates a new user for them and adds their uid and oauth token.
  3. I can then make calls to the Graph API using Facebook's Python SDK.

I did see this example. Is it that simple on normal Django?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂归处 2024-10-11 06:11:26

我的公司已经构建了一个库,使将 Facebook 集成到 Django 应用程序中变得非常简单(我们可能已经使用该库构建了 10-20 个应用程序,其中包括一些具有大量流量的应用程序,因此它已经过实际测试)。

pip install ecl-facebook==1.2.7

在您的设置中,添加 FACEBOOK_KEYFACEBOOK_SECRETFACEBOOK_SCOPEFACEBOOK_REDIRECT_URLPRIMARY_USER_MODEL< 的值/代码>。您还需要将 ecl_facebook.backends.FacebookAuthBackend 添加到 AUTHENTICATION_BACKENDS 中。例如,在 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',
)

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

希望这有帮助!

PS 您可以在此处阅读文档的其余部分。

, 'oauth_facebook_begin'), url(r'^oauth/facebook/complete

希望这有帮助!

PS 您可以在此处阅读文档的其余部分。

, 'oauth_facebook_complete'), )

希望这有帮助!

PS 您可以在此处阅读文档的其余部分。

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

Hope this helps!

P.S. You can read the rest of the docs here.

, 'oauth_facebook_begin'), url(r'^oauth/facebook/complete

Hope this helps!

P.S. You can read the rest of the docs here.

, 'oauth_facebook_complete'), )

Hope this helps!

P.S. You can read the rest of the docs here.

十六岁半 2024-10-11 06:11:26

我们在我工作的地方进行了大量 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)

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)
久隐师 2024-10-11 06:11:26

我推荐 https://github.com/egnity/fb.py。我的基于 Django 的 Facebook 应用程序很快就启动并运行了。它包含一个中间件,允许您在视图中运行如下代码:

for the user id:

user_id = request.facebook.graph().get_object("me")['id']

for the oauth token:

user_token = request.facebook.auth_token

然后您可以根据需要将上述内容添加到您的 User 模型中。要进行 Graph API 调用,您仍然可以使用 fb.py 的中间件——无需使用原始 python-sdk。上面的 user_id 代码是 Graph API 调用的完美示例。使用 fb.py 您还可以做更多事情。下载包含一个示例 django 项目,可帮助您入门。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文