问题描述:

I'm building a Django-tastypie server to communicate with mobile clients. In order to login Django server, the user must login his facebook account in the mobile client first. After the mobile client get the accessToken and facebook id, it will post AuthResouce to Django server with facebook_id(as userName) and accessToken.

in the obj_create of AuthResource

  1. Look up a user with userName.
  2. if user exists. I'll look up user and api key.
  3. if not found the user, I'll create a user.

the problem is that I don't know

  1. what the AuthResource's Authentication should I use? Authentication() or ApikeyAuthentication()?
  2. how to deal with the password attribute of User Model? set to NULL?
  3. is my code right?

class UserResource(ModelResource):

def get_object_list(self, request, *args, **kwargs):

return User.objects.filter(username=request.user.username)

class Meta:

queryset = User.objects.all()

resource_name = 'auth/user'

excludes = ['email']

authentication = ApiKeyAuthentication()

authorization = Authorization()

models.signals.post_save.connect(create_api_key, sender=User)

def determine_format(self, request):

return "application/json"

class AuthResource(ModelResource):

class Meta:

queryset = AuthInfo.objects.all()

resource_name = "auth"

authentication = Authentication()

authorization = Authorization()

def obj_create(self, bundle, **kwargs):

userId, accessToken = bundle.data['userId'], bundle.data['accessToken']

user = User.objects.filter(username=userId)

if not user:

user = User.objects.create_user(username=userId)

else:

bundle.obj = AuthInfo.objects.create_auth(userId, accessToken)

return bundle

def determine_format(self, request):

return "application/json"

网友答案:

You can use django-facebook to login to your Django-Tastypie server via a Facebook access token from your mobile client.

Example: https://gist.github.com/4574656

相关阅读:
Top