问题
I've been struggling with this for a bit, pouring over a bunch of other SO answers but can't seem to figure out how to authenticate a user to my Django site using the ApiKeyAuthentication.
In this answer the accepted answer states "Add the username and api_key parameters to your GET variables". My question is, how do I know what the api key is, if TastyPie is generating the key for me? If I wanted to create my own key, where would I put that key?
Here's my api.py:
class SystemResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
querySet = System.objects.all()
resource_name = 'system'
authentication = ApiKeyAuthentication()
When I call the URL: http://192.168.1.130:8080/systems/api/v1/user/?username=garfonzo&api_key=123456789
it fails, kicking me to my login page. Of course, the 123456789
is quite made up, I have not listed that key anywhere so in some ways it's obvious this is failing).
In the docs, it discusses either creating an Authorization header, or supplying the username/api_ky in the request. But again, if I'm supplying the api_key in the request, how do I know what that key should be? Where is it defined on the server?
Any help?
回答1:
In order to create an API Key for your Tastypie you should go to the Admin site, and in the TastyPie app, add a new API Key. Choose the user, leave the key blank, and it will autogenerate one for you ;)
来源:https://stackoverflow.com/questions/25552577/tastypie-and-django-authorization-with-apikeyauthentication