在 python 中更新访问令牌
renewing an access token in python
这是我的第一个问题,请多多包涵。我正在使用一个 API,它使用 15 分钟后过期的访问令牌进行身份验证,没有刷新令牌可以使用 in-lieu 的 re-login。到目前为止,我已经能够获取访问令牌并将其插入到 requests.get
调用中,但我似乎无法更新它并且不知道如何更新。
所有用这个 API 完成的工作,一般来说,都是用 Python 完成的,所以我希望将它保存在 Python 中并在同一个文件中。
我在 15 分钟后收到 401
消息代码,如果成功则代码 200
。到目前为止,我唯一的想法是将它放在更新的计时器上,但我无法对 Whosebug 帖子或相关文档做出正面或反面的描述,在单独的脚本中登录 运行,然后该脚本调用另一个一个用于当前 header 变量(但仍然需要一个计时器),或者让它在遇到 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 供其他人试用的更多详细信息。是non-public
最终我找到了我自己问题的答案。为以后的用户发布这个。更新的片段如下。
故事的简短版本: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
这是我的第一个问题,请多多包涵。我正在使用一个 API,它使用 15 分钟后过期的访问令牌进行身份验证,没有刷新令牌可以使用 in-lieu 的 re-login。到目前为止,我已经能够获取访问令牌并将其插入到 requests.get
调用中,但我似乎无法更新它并且不知道如何更新。
所有用这个 API 完成的工作,一般来说,都是用 Python 完成的,所以我希望将它保存在 Python 中并在同一个文件中。
我在 15 分钟后收到 401
消息代码,如果成功则代码 200
。到目前为止,我唯一的想法是将它放在更新的计时器上,但我无法对 Whosebug 帖子或相关文档做出正面或反面的描述,在单独的脚本中登录 运行,然后该脚本调用另一个一个用于当前 header 变量(但仍然需要一个计时器),或者让它在遇到 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 供其他人试用的更多详细信息。是non-public
最终我找到了我自己问题的答案。为以后的用户发布这个。更新的片段如下。
故事的简短版本: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