【问题标题】:Getting OAuth2 token from ansible tower with python?使用 python 从 ansible 塔获取 OAuth2 令牌?
【发布时间】:2020-02-05 02:53:17
【问题描述】:

我正在尝试使用此 python 脚本获取/创建 OAuth2 访问令牌:

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)

当我运行脚本时出现此错误:

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

我已经查看了这里的文档:https://docs.ansible.com/ansible-tower/latest/html/administration/oauth2_token_auth.html,但我无法弄清楚。

请帮忙

更新:

当我执行这个 python 代码时:

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)

我得到这个输出:

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

更新二:

好的,这段代码得到了令牌:

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}')

【问题讨论】:

    标签: ansible-tower


    【解决方案1】:

    根据官方文档。 您可以使用以下 curl 命令创建 OAuth 2 令牌。

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

    可以使用请求包翻译成Python

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

    生成令牌后,可以通过将其添加到请求标头Authorization: Bearer &lt;oauth2-token-value&gt; 中来将其用于其他请求。

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

    更新: 您将数据作为 json 传递并将标头的 Content-Type 属性设置为 application/x-www-form-urlencoded,它应该是 application/json

    【讨论】:

    • 我使用的是 AWX 7.0.0。抱歉,我无法分享实际的网址。但它是 AWX 的基本安装。
    • 哦,好的。如果您通过 api - github.com/ansible/awx/blob/devel/docs/auth/oauth.md 遵循相同的创建应用程序,您可以查看此页面吗?
    • 是的,我已经看过了,无论我做什么,我都会收到 401 b'{"detail":"Authentication credentials were not provided. To establish a login session, visit /api/login/."}' 错误响应
    • 您是否尝试过使用基本授权发帖到https://&lt;tower-host&gt;/api/v2/login/?因为对于 tower api 文档,我无法看到带有 /api/login 的任何端点。
    • 我在这里很尴尬。我使用了错误的密码。 红脸
    猜你喜欢
    • 1970-01-01
    • 2015-07-27
    • 2013-08-03
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多