def authorise():
# Define 'headertokenonly' and 'headers' as global variables, with actual values defined below:
global headertokenonly
global headers
# Define 'headertokenonly' global authentication variable - read from /home/pi/10cv4/authtoken.txt,
# ***The text file MUST exist and contain one line, ONLY the text of the auth token,***
# It's returned from the API at the end of the 'Login' subroutine,
# This header contains only the token - used when only an auth header is required,
# First check if it exists,
# If it does not, ask the user to login for the first time:
try:
open("authtoken.txt")
except IOError as e:
os.system('clear')
print("")
print(Fore.BLACK + Back.WHITE + "10cbazbt3:")
print("")
print(Fore.BLACK + Back.RED + "Please Login for the first time." + Style.RESET_ALL)
print("")
login()
tempinput = input(Fore.YELLOW + "Please press [enter] to continue" + Style.RESET_ALL)
# Then carry on creating the authorisation headers:
authtokenonlyfile = open("authtoken.txt", "r")
authtokenonly = authtokenonlyfile.read()
headertokenonly = {'Authorization': authtokenonly}
# Define the 'headers' global variable using 'authtokenonly' from above,
# This header is used throughout the application:
headers = {'Authorization': authtokenonly, 'Content-Type': 'application/x-www-form-urlencoded'}
# Define the 'login' subroutine:
# Getting and applying the auth token is unnecessarily long,
# Requires less of the file i/o:
评论列表
文章目录