2

I am a OpenStack noob here.

I try to use REST API to automate my openstack work. I can successfully authenticate my account and get token from curl with below command:

export AUTH_URL="https://my_url:5000/v2.0/tokens"
curl -v -X POST $AUTH_URL -d '{"auth":{"passwordCredentials":{"username": "myusername", "password":"myuser_password"}, "tenantId":"my_tenant_id"}}' -H 'Content-type: application/json' | python -m json.tool

But, when I try to use Pyton, with this code, it gave me this error:

Traceback (most recent call last):
  File "./test.py", line 10, in <module>
    keystone = ksclient.Client(auth_url=auth_url, username=user_name, password=user_pwd, tenant_name=tenant_name)
  File "/usr/lib/python2.7/site-packages/keystoneclient/v2_0/client.py", line 176, in __init__
    self.authenticate()
  File "/usr/lib/python2.7/site-packages/positional/__init__.py", line 101, in inner
    return wrapped(*args, **kwargs)
  File "/usr/lib/python2.7/site-packages/keystoneclient/httpclient.py", line 581, in authenticate
    resp = self.get_raw_token_from_identity_service(**kwargs)
  File "/usr/lib/python2.7/site-packages/keystoneclient/v2_0/client.py", line 220, in get_raw_token_from_identity_service
    _("Authorization Failed: %s") % e)
keystoneauth1.exceptions.auth.AuthorizationFailure: Authorization Failed: The resource could not be found. (HTTP 404) (Request-ID: 

My python code is below:

#!/usr/bin/env python

import keystoneclient.v2_0.client as ksclient

auth_url  = "https://my_url:5000/v2.0/tokens"
user_name = "myusername"
user_pwd  = "myuser_password"
tenant_name = "my_tenant_id"

keystone = ksclient.Client(auth_url=auth_url, username=user_name, password=user_pwd, tenant_name=tenant_name)

print keystone.auth_token

Can you please take a look at my code and see why the python code does not work?

Thanks!

Betty
  • 33
  • 5

1 Answers1

1

You should only provide https://my_url:5000/v2.0 as the path when authenticating using the client.

See this documentation for more information.

You could also try something like this.

from keystoneauth1 import loading
from keystoneauth1 import session
from keystoneclient import client as keystoneclient

auth_url = 'https://my_url:5000'
user = '<user>'
password = '<password>'
tenant = '<tenant>'

loader = loading.get_plugin_loader('password')

keystone_auth = \
    loader.load_from_options(
        auth_url=auth_url,
        username=user,
        password=password,
        user_domain_name=tenant
    )

keystone_session = session.Session(
    auth=keystone_auth,
    verify=False
)

print(keystone_session.get_token())

Keep in mind that the Keystone V2 API has been deprecated, and removed in the latest versions of Openstack. If possible I would recommend moving over to the V3 API as soon as possible.

eandersson
  • 25,781
  • 8
  • 89
  • 110
  • I met the same issue. I have the same URL as you mentioned. But it returns me HTTP 401 error (The request you have made requires authentication). I am sure that the username/password are correct as I can authenticate with curl commands. Please help! Thank you! I have the piece of code copied from here: https://docs.openstack.org/python-keystoneclient/latest/using-api-v2.html (with changed my username/password, url, and tenant) – Howard Shane Sep 12 '18 at 18:41
  • What version of Openstack are you running? also what version of the keystoneclient? – eandersson Sep 12 '18 at 21:37
  • Make sure to use an older version of the keystoneclient, as v2 is no longer supported. – eandersson Sep 12 '18 at 23:23