I'm trying to develop a REST provider with OAuth. I'm using Django RESTFramework and DjangoOAuthToolkit. I did a GET and it works perfectly but I'm trying to use a POST and the server responds with {"detail": "Method 'POST' not allowed."}
This is my code:
# views.py
@api_view(['POST'])
def pruebapost(request):
usuario = User()
access_token = Token.objects.get(
key=request.POST['oauth_token']
)
usuario = access_token.user
content = {'saludo': usuario.username}
return Response(content)
# settings.py
OAUTH_AUTHORIZE_VIEW = 'principal.views.oauth_authorize'
SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'
REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
'DEFAULT_PARSER_CLASSES': (
'rest_framework.parsers.JSONParser',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.OAuthAuthentication',
),
}
And I'm using this as a "test" client:
import urlparse
import oauth2 as oauth
import requests
consumer_key = "clave"
consumer_secret = "secreto"
consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
resource_url = 'http://blablabla.pythonanywhere.com/prueba'
consumer = oauth.Consumer(key='clave', secret='secreto')
token = oauth.Token(key='e7456187a43141af8d2e0d8fa99b95b9',
secret='3wRIKoacff16tcew')
oauth_request = oauth.Request.from_consumer_and_token(
consumer,
token,
http_method='POST',
http_url=resource_url,
parameters={'hola':'pepe'}
)
oauth_request.sign_request(
oauth.SignatureMethod_HMAC_SHA1(),
consumer,
token
)
url = oauth_request.to_url()
response = requests.post(url, oauth_request.to_postdata())
print response.content
I don't understand what REST Framework documentation says about 405 Method not allowed
"Raised when an incoming request occurs that does not map to a handler method on the view."
Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…