TastyPie and Django authorization with ApiKeyAuthentication
Asked Answered
S

1

2

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?

Steffi answered 28/8, 2014 at 15:27 Comment(0)
S
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 ;)

Scape answered 28/8, 2014 at 15:57 Comment(3)
Ah, nice that's where I create the api_key. So next question, if I have a middleware that kicks you to the login page if you haven't logged in, can TastyPie deal with? Somehow tell Django that this user can access the data. Isn't that the point of ApiKeyAuthentication?Steffi
Well that's a totally different question. You should open another question, and I will try to answer there. This way people will be able to find both questions without looking in the comments on a totally unrelated one ;) And also, if this answer helped, please mark it as accepted! CheersScape
Thanks, your solution did help. I've opened another post at #25573537Steffi

© 2022 - 2024 — McMap. All rights reserved.