Search code examples
ansible-tower

Getting OAuth2 token from ansible tower with python?


I am trying to get/create an OAuth2 access token with this python script:

import requests
import json

token_url = 'https://mytower.example.com/api/v2/tokens/'

data = {
    "description": "My Access Token",
    "application": 2,
    "scope": "write"
}

client_id = "I457Uue7...Ikdiafhjd"
client_secret = "Xvcgsh8...Ikadfi84"

user = 'myaccount'
passwd = 'that works in the UI'
# Get token
params = {"grant_type": "password", "username": user, "password": passwd}
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(token_url, auth=(client_id, client_secret), headers=headers, params=params, verify=False)
print(f'{response.status_code} {response.content}')
if response.status_code == 200:
  token = response.json()['access_token']
  print(token)

When I run the script I get this error:

401 b'{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}'

I have looked at the documentation here: https://docs.ansible.com/ansible-tower/latest/html/administration/oauth2_token_auth.html but I can't figure it out.

Please help

UPDATE:

When I execute this python code:

import requests
import json

token_url = 'https://mytower.example.com/api/v2/tokens/'

headers = {"Content-Type": "application/json"}

user = 'me'
passwd = 'mypasswd'

# Get token
response = requests.post(token_url, verify=False, auth=(user, passwd))
print(f'{response.status_code} {response.content}')
token = response.json()['access_token']
print(token)

I get this output:

401 b'{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}'

UPDATE II:

Ok this code gets the token:

import requests

token_url = 'https://mytower.example.com/api/v2/tokens/'

data = {
    "description": "My Access Token",
    "application": 2,
    "scope": "write"
}

headers = { 'Content-Type': 'application/json' }
gen_user = 'me'
gen_pass = 'mypassword'

# Get token
response = requests.post(token_url, auth=(gen_user, gen_pass), headers=headers, json=data, verify=False)
print(f'{response.status_code} {response.content}')

Solution

  • As per the official documentation. You can create a OAuth 2 token using the below curl command.

    curl -u user:password -k -X POST https://<tower-host>/api/v2/tokens/
    

    which can be translated to Python using request package by

    import requests
    
    response = requests.post('https://<tower-host>/api/v2/tokens/', 
        verify=False, auth=('user', 'password'))
    

    Once token is generated then it can be used in other requests by adding it in request header Authorization: Bearer <oauth2-token-value>.

    Reference: https://www.ansible.com/blog/summary-of-authentication-methods-in-red-hat-ansible-tower

    Update: You are passing data as json and setting header's Content-Type property to application/x-www-form-urlencoded, which should be application/json.