How could I authenticating and receive a token in the python script to use with subequent API requests?

asked 2020-04-20 03:36:52 -0500

OS_RM gravatar image

How could I based on username/pass authenticating and receive a token in the python script to use with subequent API requests? I have been using this

#!/usr/bin/env python3

import json
import requests as req

api_url = 'http://AUTH_URL:5000/v3/auth/tokens'
headers = {'Content-Type': 'application/json'}
data = { "auth": {
    "identity": {
      "methods": ["password"],
      "password": {
        "user": {
          "name": "admin",
          "domain": { "id": "default" },
          "password": "adminpwd"
        }
      }
    }
  }
}
resp = req.post(api_url, headers = headers, json = data)

print(resp.json())

but the response is the following:

{"error": {"message": "The request you have made requires authentication.", "code": 401, "title": "Unauthorized"}}

Many thanks for your help!

edit retag flag offensive close merge delete