9

我有一个允许用户通过 Facebook 注册的移动应用程序。从 FB 收到访问令牌后,我将其发送到 Django 后端。

但是然后呢?

django-allauth 内部是否有一种方法可以提供访问令牌以使其创建新的用户/社交帐户?

还是我需要手动完成所有操作?

4

3 回答 3

7

我似乎总是在发布问题后立即找出答案。无论如何,以下是 UserResource 上的自定义方法(使用 ApiKeyAuthentication 的 tastypie api)。其中大部分来自 allauth.socialaccount.providers.facebook.views 中的 login_by_token 方法。

我暂时不会选择我的答案是正确的,以防有人发布更好的答案。

def facebook_login(self, request, **kwargs):
    self.method_check(request, allowed=['post'])

    data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))

    access_token = data.get('access_token', '')

    from allauth.socialaccount import providers
    from allauth.socialaccount.models import SocialLogin, SocialToken, SocialApp
    from allauth.socialaccount.providers.facebook.views import fb_complete_login
    from allauth.socialaccount.helpers import complete_social_login
    try:
        app = SocialApp.objects.get(provider="facebook")
        token = SocialToken(app=app,
                            token=access_token)
        login = fb_complete_login(app, token)
        login.token = token
        login.state = SocialLogin.state_from_request(request)
        ret = complete_social_login(request, login)

        #if we get here we've succeeded
        return self.create_response(request, {
                'success': True,
                'username': request.user.username,
                'user_id': request.user.pk,
                'api_key': request.user.api_key.key,
                } ) 
    except:
        # FIXME: Catch only what is needed
        return self.create_response(request, {
                'success': False,
                'reason': "Bad Access Token",
                }, HttpForbidden ) 
于 2013-05-05T07:14:27.043 回答
2

现代读者(2017)可能想看看 django-rest-auth,它建立在 allauth 之上,但考虑到了单页应用程序:

http://django-rest-auth.readthedocs.io/en/latest/introduction.html

于 2017-03-16T21:49:32.203 回答
1

工作源代码:在类 UserResource(ModelResource):

def override_urls(self):
    return [
        url(r"^(?P<resource_name>%s)/facebook_login%s$" %
            (self._meta.resource_name, trailing_slash()),
            self.wrap_view('facebook_login'), name="api_facebook_login"),
    ]

def facebook_login(self, request, **kwargs):
    self.method_check(request, allowed=['post'])

    data = self.deserialize(
        request,
        request.raw_post_data,
        format=request.META.get('CONTENT_TYPE', 'applicaton/json'))

    access_token = data.get('access_token', '')

    print(access_token)

    from allauth.socialaccount import providers
    from allauth.socialaccount.models import SocialLogin, SocialToken, SocialApp
    from allauth.socialaccount.providers.facebook.views import fb_complete_login
    from allauth.socialaccount.helpers import complete_social_login
    try:
        app = SocialApp.objects.get(provider="facebook")
        print(app)
        token = SocialToken(app=app,
                            token=access_token)
        login = fb_complete_login(app, token)
        login.token = token

        login.state = SocialLogin.state_from_request(request)
        ret = complete_social_login(request, login)

        #if we get here we've succeeded
        return self.create_response(request, {
                'success': True,
                'username': request.user.username,
                'user_id': request.user.pk,
                'api_key': request.user.api_key.key,
                } )
    except:
        # FIXME: Catch only what is needed
        return self.create_response(request, {
                'success': False,
                'reason': "Bad Access Token",
                }, HttpForbidden )

然后:

curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"access_token":"xxxxxxxx"}' http://xx.xxxxxx.com:8000/api/v1 /user/facebook_login/

测试OK,创建用户OK。

谢谢你。

于 2013-06-27T03:09:47.910 回答