简体   繁体   中英

python set returned value from another function as a variable to use in header

Trying to get token as a variable in auth header in request in submit_documents() function it works fine if it was hard coded; however I need to get from the return from log_in_as_taxpayer() function:

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}' , notice the leading f . See here and here for more info or search python f-string with your favorite search engine;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM