Ask Your Question
0

keystone REST API and python [closed]

asked 2014-06-09 17:41:04 -0500

conrosebraugh gravatar image

updated 2014-06-09 17:52:07 -0500

I am running Havana, and I have verified the install of install of the identity service by using running the following:

keystone --os-username=uname --os-password=pword --os-auth-url=http://localhost:35357/v2.0 token-get

which returns the token just fine. When I run the following python script on another machine on my network I get an HTTP 401 when using port 9696 and HTTP 400 when using port 35357.

import requests

username = 'uname'

password = 'pword'

payload = {'auth': {'tenantName': 'tenant1', 'passwordCredentials': {'username': uname, 'password': pword}}}

print requests.post('http://10.20.30.40:9696/v2.0/tokens', data=payload)

What am I missing? The guide I am using is http://docs.openstack.org/api/openstack-identity-service/2.0/content/Request_Response_Types-d1e149.html (here).

EDIT: I have also tried using curl and the following command: url -d '{"auth":{“tenantName": “tenant1", "passwordCredentials": {"username": “uanme", "password": “pword"}}}' -H "Content-type: application/json" http://10.20.30.40:35357/v2.0/tokens

but that returns the following: {"error": {"message": "Expecting to find valid JSON in request body. The server could not comply with the request since it is either malformed or otherwise incorrect. The client is assumed to be in error.", "code": 400, "title": "Bad Request"}}

I got that curl command from http://docs.openstack.org/developer/keystone/api_curl_examples.html (http://docs.openstack.org/developer/k...) .

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by conrosebraugh
close date 2014-06-10 13:25:18.748469

1 answer

Sort by » oldest newest most voted
1

answered 2014-06-09 19:57:47 -0500

larsks gravatar image

updated 2014-06-09 19:59:40 -0500

Your first problem is that Keystone doesn't listen on port 9696 (that's Neutron). However, it does listen on port 35357 (as well as port 5000), and your problem stems from the fact you are not sending Keystone the data in the format it expects.

If you read the requests documentation, you will find that when you pass a dictionary to the data parameter of requests.post that it form-encodes the data. Keystone wants a JSON-encoded payload. So you need this:

res = requests.post('http://localhost:5000/v2.0/tokens', 
                headers={'content-type': 'application/json'},
                data=json.dumps(payload))

Another way of resolving this sort of problem is enabling debugging in the requests library (see this question on StackOverflow), and checking how the request that you're making compares to what you see when running the keystone client with --debug.

edit flag offensive delete link more

Comments

Thank you! I thought I read in the docs that the default content-type that the service expects is json, but that must have been the default return type.

conrosebraugh gravatar imageconrosebraugh ( 2014-06-10 10:24:21 -0500 )edit

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-06-09 17:41:04 -0500

Seen: 2,889 times

Last updated: Jun 09 '14