I am sort of stuck while creating something very simple - a new user.
Workflow in my head is this: User sents query with the api key. Checks if the user exists or not. if not, create the user.
This is my UserSignUpResource:
class UserSignUpResource(ModelResource):
class Meta:
object_class = User
queryset = User.objects.all()
allowed_methods = ['post']
include_resource_uri = False
resource_name = 'newuser'
excludes = ['is_active','is_staff','is_superuser']
authentication = ApiKeyAuthentication()
authorization = DjangoAuthorization()
models.signals.post_save.connect(create_api_key, sender=User)
def obj_create(self,bundle,request=None,**kwargs):
try:
bundle = super(UserSignUpResource, self).obj_create(bundle,request,**kwargs)
bundle.obj.set_password(bundle.data.get('password'))
bundle.obj.save()
except IntegrityError:
raise BadRequest('The username already exists')
return bundle
def apply_authorization_limits(self,request,object_list):
return object_list.filter(id=request.user.id,is_superuser=True)
My api call:
curl -v -X POST -d '{"username" : "puck", "password" : "123456"}' -H "Authorization: ApiKey puck:link" -H "Content-Type: application/json" http://127.0.0.1:8000/api/v1/newuser/
I get a 401 error. That is precisely because I don't know how to create an API key so that I can use it to create new users.
I can see a api column in django admin but it allows me to only create one api key per user. Thus, how does that work? How do I make it work?
Very little information on the tutorials. Hardly any blogs about it. Any help would be nice. I am struggling quite bad.
You can create ApiKey with:
ApiKey.objects.create(user=user)
(you can do it through admin interface as well, but be careful that saving empty inline won't actually save it, ie: change date/time field of api key inline).
After you have api_key, this should work:
curl -v -X POST -d '{"username" : "puck", "password" : "123456"}' -H "Content-Type: application/json" http://127.0.0.1:8000/api/v1/newuser/?username=user&api_key=API_KEY