PythonSet从另一个函数返回值,作为要在标头中使用的变量
在submit_documents()函数中,尝试在请求的auth头中获取令牌作为变量,如果它是硬编码的,则可以正常工作;但是,我需要从log_in_as_纳税人()函数的返回中获取:PythonSet从另一个函数返回值,作为要在标头中使用的变量,python,class,authentication,header,access-token,Python,Class,Authentication,Header,Access Token,在submit_documents()函数中,尝试在请求的auth头中获取令牌作为变量,如果它是硬编码的,则可以正常工作;但是,我需要从log_in_as_纳税人()函数的返回中获取: import json class ETax_Integration(object): """docstring for ETax_Integration""" def __init__(self, arg): supe
import json
class ETax_Integration(object):
"""docstring for ETax_Integration"""
def __init__(self, arg):
super(ETax_Integration, self).__init__()
self.arg = arg
def log_in_as_taxpayer(self):
client_id='###'
# print(client_id)
url = "https://url/connect/token"
client_secret='###'
# print(client_secret)
payload={'grant_type':'client_credentials','client_id':{client_id},'client_secret':{client_secret},'scope':'InvoicingAPI'}
headers = {
'Content-Type':'application/x-www-form-urlencoded'
}
response = requests.request("POST", url, headers=headers, data=payload, verify=False)
values=json.loads(response.text)
# print(values['access_token'])
# print( response.raise_for_status())
return (values['access_token'])
def submit_document(self):
token=self.log_in_as_taxpayer()
print(token)
submit_payload = json.dumps({
"documents": []
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {token}'
}
submit_url = "https://api/link"
response = requests.request("POST", submit_url, headers=headers, data=submit_payload,verify=False)
print(response.text)
print( response.raise_for_status())
test=ETax_Integration(object)
# test.log_in_as_taxpayer()
test.submit_document()
f'Bearer{token}
,注意前面的f
。有关更多信息,请参阅和,或使用您喜爱的搜索引擎搜索python f-string
)