在python中更新访问令牌 - python

这是我的第一个问题,请忍受。我正在使用一个使用15分钟后到期的访问令牌进行身份验证的API,没有刷新令牌可用于重新登录的环境。到目前为止,我已经能够获取访问令牌并将其插入到requests.get调用中,但是我似乎无法使它续订,并且对方法一无所知。
与此API一起完成的所有工作,通常是与Python一起完成的,因此我希望在整个Python中并将其保存在同一文件中。

15分钟结束后,我会得到一个401消息代码,如果成功,则得到代码200。到目前为止,我唯一的想法是将其放在计时器上进行更新,但是我无法在堆栈溢出帖子或有关此操作的文档上留下头或尾,让登录在单独的脚本中运行,然后此脚本为当前脚本调用另一个标头变量(但是仍然需要一个计时器),或者在遇到response.status_code != 200后调用它来重做登录功能。

获取访问令牌的示例脚本

import requests, os, json, time, csv
def login (url, payload):
    #this will log into API and get an access token
    auth = requests.post(url, data=payload).json()
    sessionToken = auth["token"]
    sessionTimer = auth["validFor"]
    headers = {'Access-Token': sessionToken}
    return headers
#calling the function to generate the token
if __name__ == '__main__':
    url = "url inserted here"
    u = input("Enter your username: ")
    p = input("Enter your password: ")
    t = input("Enter your tenancy name: ")
    payload = {'username': u, 'password': p, 'tenant': t}
    print("Logging in")
    headers = login(url, payload)
#the actual work as pulled from a csv file
valuables = input("CSV file with filepath: ")
file = open(valuables, 'r', encoding='utf-8')
csvin = csv.reader(file)
for row in csvin:
    try:
        uuidUrl = row[0]
        output_file = row[1]
        response = requests.get(uuidUrl, headers=headers)
        print(response.status_code)
        with open(output_file, 'wb') as fd:
            for chunk in response.iter_content(chunk_size=128):
                fd.write(chunk)
        fd.close()
    except requests.exceptions.RequestException:
        print(output_file,"may have failed")
        login(url, payload)
        continue

我无法成功识别if response.status_code != 200:作为回调login()的方法。我似乎也无法使它退出while True:循环。

抱歉,我无法提供其他有关访问API的详细信息,以供其他人试用。这是非公开的

参考方案

最终,我能够弄清楚自己的问题的答案。将其发布给以后的用户。更新的代码段如下。

故事的简短版本:requests.status_code发送回一个整数,但是我错误地假设它将是一个字符串,因此我的内部比较不好。

for row in csvin:
    try:
        uuidUrl = row[0]
        xip_file = row[1]
        response = requests.get(uuidUrl, headers=headers)
        status = response.status_code
        print(status)
        if status == 401:
            print(xip_file, "may have failed, loggin back in")
            login(url, payload)
            headers = login(url, payload)
            response = requests.get(uuidUrl, headers=headers)
            with open(xip_file, 'wb') as fd:
                for chunk in response.iter_content(chunk_size=128):
                    fd.write(chunk)
            fd.close()
        else:
            with open(xip_file, 'wb') as fd:
                for chunk in response.iter_content(chunk_size=128):
                    fd.write(chunk)
            fd.close()
    except requests.exceptions.RequestException:
        print(xip_file,"may have failed")
        headers = login(url, payload)
        continue

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python-使用请求时发布请求失败 - python

使用外壳程序时,我可以通过运行以下命令成功创建新用户curl --user administrator:pasword "Content-Type: application/json" https://localhost:8080/midpoint/ws/rest/users -d @user.json但是,当我尝试使用请求在python…

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

无法注释掉涉及多行字符串的代码 - python

基本上,我很好奇这为什么会引发语法错误,以及如何用Python的方式来“注释掉”我未使用的代码部分,例如在调试会话期间。''' def foo(): '''does nothing''' ''' 参考方案 您可以使用三重双引号注释掉三重单引…