【问题标题】:python set returned value from another function as a variable to use in headerpython将另一个函数的返回值设置为要在标头中使用的变量
【发布时间】:2021-07-02 06:04:09
【问题描述】:

尝试在 submit_documents() 函数的请求中获取令牌作为 auth 标头中的变量,如果它是硬编码的,它可以正常工作;但是我需要从 log_in_as_taxpayer() 函数的返回中获取:

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()

【问题讨论】:

    标签: python class authentication header access-token


    【解决方案1】:

    f'Bearer {token}',注意前面的f。请参阅herehere 了解更多信息或使用您最喜欢的搜索引擎搜索python f-string ;)

    【讨论】:

      猜你喜欢
      • 2022-01-14
      • 2016-11-25
      • 2022-01-22
      • 1970-01-01
      • 2015-10-18
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多