2012-11-12 78 views
2

我正在構建一個Django-tastypie服務器來與移動客戶端進行通信。爲了登錄Django服務器,用戶必須先在移動客戶端登錄他的Facebook賬戶。在移動客戶端獲得accessToken和facebook id之後,它將使用facebook_id(作爲用戶名)和accessToken將AuthResouce發佈到Django服務器。如何使用tastypie登錄Facebook?

在AuthResource

  1. 查找與用戶名的用戶的obj_create。
  2. 如果用戶存在。我會查找用戶和API密鑰。
  3. 如果找不到用戶,我會創建一個用戶。

的問題是,我不知道

  1. 的AuthResource的認證,我應該用什麼呢? Authentication()或ApikeyAuthentication()?
  2. 如何處理用戶模型的密碼屬性?設置爲NULL?
  3. 是我的代碼對不對?

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" 
+0

見本:https://github.com/toastdriven/django-tastypie/pull/315 – Ahsan

回答