如何让我的 get_access_token 变成 运行 只有一次?
How to get my get_access_token to run only once?
我试图让 get_access_token 函数只执行一次,然后将令牌用于后续请求。但是好像每次调用token变量都是运行ning。请提供有关如何将令牌存储在变量中并在必须调用或 运行 get_access_token 函数的函数之外使用它的建议。
输出打印如下:
令牌是 xxxxx
API1 响应
令牌是 yyyy
API2 响应
class api:
def get_access_token(token):
url = env.base_url
para = {"acctId": env.acct_id, "secret": env.secret_key}
head = {"x-api-key": env.x_api_key}
try:
r = requests.get(url, params=para, headers=head)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
token = str(dict['token'])
expires = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(dict['expireTime']))
print('Token acquired successfully ' + token)
return token
except:
print('Data error in access token')
print(dict)
def get_driver_overview(self):
url = env.base_url
head = {"x-api-key": env.x_api_key, "token": public_api.get_access_token()}
body = {"acctId": str(env.acct_id), "driverId": env.driver_id}
try:
r = requests.post(url, headers=head, json=body)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
print(dict)
except:
print("Error in driver_overview")
print(dict)
def get_device_list(self):
url = env.base_url
head = {"x-api-key": env.x_api_key, "token": public_api.get_access_token()}
body = {"acctId": str(env.acct_id)}
try:
r = requests.post(url, headers=head, json=body)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
print(dict)
except:
print("Error in device list api")
print(dict)
public_api = api()
public_api.get_driver_overview()
public_api.get_device_list()
您可以将访问令牌保存在环境变量中。
env.access_token = public_api.get_access_token()
然后,通过访问环境变量而不是每次调用函数将其附加到header
head = {"x-api-key": env.x_api_key, "token": env.access_token}
我试图让 get_access_token 函数只执行一次,然后将令牌用于后续请求。但是好像每次调用token变量都是运行ning。请提供有关如何将令牌存储在变量中并在必须调用或 运行 get_access_token 函数的函数之外使用它的建议。
输出打印如下:
令牌是 xxxxx
API1 响应
令牌是 yyyy
API2 响应
class api:
def get_access_token(token):
url = env.base_url
para = {"acctId": env.acct_id, "secret": env.secret_key}
head = {"x-api-key": env.x_api_key}
try:
r = requests.get(url, params=para, headers=head)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
token = str(dict['token'])
expires = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(dict['expireTime']))
print('Token acquired successfully ' + token)
return token
except:
print('Data error in access token')
print(dict)
def get_driver_overview(self):
url = env.base_url
head = {"x-api-key": env.x_api_key, "token": public_api.get_access_token()}
body = {"acctId": str(env.acct_id), "driverId": env.driver_id}
try:
r = requests.post(url, headers=head, json=body)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
print(dict)
except:
print("Error in driver_overview")
print(dict)
def get_device_list(self):
url = env.base_url
head = {"x-api-key": env.x_api_key, "token": public_api.get_access_token()}
body = {"acctId": str(env.acct_id)}
try:
r = requests.post(url, headers=head, json=body)
rf = json.dumps(r.json(), indent=4, sort_keys=True)
dict = json.loads(rf)
assert r.status_code == 200
print(dict)
except:
print("Error in device list api")
print(dict)
public_api = api()
public_api.get_driver_overview()
public_api.get_device_list()
您可以将访问令牌保存在环境变量中。
env.access_token = public_api.get_access_token()
然后,通过访问环境变量而不是每次调用函数将其附加到header
head = {"x-api-key": env.x_api_key, "token": env.access_token}