我有一个Openshift 3.11集群,默认安装Prometheus和Alertmanager。我想写一个python脚本来抓取Alertmanager API端点,这样我就可以解析数据并将其传递给我们的运营团队的第三方监控工具。我的问题是,为了访问API,我需要对oauth进行身份验证。我如何在python中做到这一点呢?
发布于 2020-04-15 03:07:23
我不知道与Reddit API相比,这对警报管理器是否有什么不同,但当我为此设置一个机器人时,我必须首先在他们的OAuth页面上注册它,然后我需要使用它提供给我的代码,以请求访问令牌和我的其余用户数据(登录和密码以及应用程序的调用),然后我可以使用该身份验证来联系API端点。
下面是我从Reddit请求访问令牌的函数:
def AuthRequest():
import requests
import requests.auth
import json
client_auth = requests.auth.HTTPBasicAuth('Application ID code', 'OAuth secret code')
post_data = {"grant_type": "password", "username": "Your_Username", "password": "Your_Password"}
headers = {"User-Agent": "Name_Of_Application"}
response = requests.post("https://www.reddit.com/api/v1/access_token", auth=client_auth, data=post_data, headers=headers)
return response.json()下面是与端点联系并从中获取数据的代码:
import requests
import requests.auth
import json
currentComments = []
headers = {"Authorization": auth['token_type'] + " " + auth['access_token'], "User-Agent": "Your_Application_Name"}
mentions = requests.get("https://oauth.reddit.com/message/unread.json", headers=headers).json()请注意,这里的“auth”只是来自身份验证令牌的“响应”。我希望这会有帮助,我真的不知道这和never管理器有什么不同,因为我从来没有真正用过它。
发布于 2020-04-23 01:38:53
我找到了适合我的方法。
我需要创建一个服务帐户
oc create serviceaccount <serviceaccount name> -n <your-namespace>然后为其创建集群角色绑定
oc create clusterrolebinding <name for your role> \
--clusterrole=cluster-monitoring-view \
--serviceaccount=<your-namespace>:<serviceaccount name>从SA获取令牌,然后在curl中使用该令牌
oc sa get-token <serviceaccount name> -n <your-namespace>https://stackoverflow.com/questions/61215196
复制相似问题