TypeError:load_pem_private_key()缺少1个必需的位置参数:“后端”

2024-05-23 08:00:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用adobe API2.0提取adobe analytics数据,我是这方面的新手,所以在这repo之后,我提供了所有详细信息,例如APIKEY、techaccountID、org_id、client secret、modified config.ini。在生成JWT令牌时,我得到了以下错误

TypeError: load_pem_private_key() missing 1 required positional argument: 'backend'

这是我的密码

def get_jwt_token(config):
    with open(config["key_path"], 'r') as file:
        private_key = file.read()

    return jwt.encode({
        "exp": datetime.datetime.utcnow() + datetime.timedelta(seconds=30),
        "iss": config["orgid"],
        "sub": config["technicalaccountid"],
        "https://{}/s/{}".format(config["imshost"], config["metascopes"]): True,
        "aud": "https://{}/c/{}".format(config["imshost"], config["apikey"])
    }, private_key, algorithm='RS256')


config = dict(config_parser["default"])
jwt_token = get_jwt_token(config)
logger.info("JWT Token: {}".format(jwt_token))
access_token = get_access_token(config, jwt_token)
logger.info("Access Token: {}".format(access_token))

这是错误消息

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-8c61bcf6ee58> in <module>
      1 config = dict(config_parser["default"])
----> 2 jwt_token = get_jwt_token(config)
      3 logger.info("JWT Token: {}".format(jwt_token))
      4 access_token = get_access_token(config, jwt_token)
      5 logger.info("Access Token: {}".format(access_token))

<ipython-input-3-d22e1d6f4ebb> in get_jwt_token(config)
      9         "https://{}/s/{}".format(config["imshost"], config["metascopes"]): True,
     10         "aud": "https://{}/c/{}".format(config["imshost"], config["apikey"])
---> 11     }, private_key, algorithm='RS256')

~\AppData\Local\Continuum\anaconda3\lib\site-packages\jwt\api_jwt.py in encode(self, payload, key, algorithm, headers, json_encoder)
     61         ).encode("utf-8")
     62 
---> 63         return api_jws.encode(json_payload, key, algorithm, headers, json_encoder)
     64 
     65     def decode_complete(

~\AppData\Local\Continuum\anaconda3\lib\site-packages\jwt\api_jws.py in encode(self, payload, key, algorithm, headers, json_encoder)
    108         try:
    109             alg_obj = self._algorithms[algorithm]
--> 110             key = alg_obj.prepare_key(key)
    111             signature = alg_obj.sign(signing_input, key)
    112 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\jwt\algorithms.py in prepare_key(self, key)
    248                         key = load_ssh_public_key(key)
    249                     else:
--> 250                         key = load_pem_private_key(key, password=None)
    251                 except ValueError:
    252                     key = load_pem_public_key(key)

TypeError: load_pem_private_key() missing 1 required positional argument: 'backend'

我尝试了本视频中指定的不同方法,但所述方法也导致了相同的错误 https://www.youtube.com/watch?v=eSh2r3ZTCQU

我用谷歌搜索了一下,但没有找到解决方案。从错误中,我可以解释我应该提供一个参数backend,但是我应该在哪里提供呢?谁能帮帮我这里怎么了


Tags: keyinhttpstokenconfigformatgetaccess
2条回答

我也有同样的错误,但我的树莓Pi项目。我厌倦了Google Cloud IoT核心的python示例。如果有人面临同样的问题,我会分享我的经验

我尝试了pyjwt和加密技术的不同版本组合,但问题仍然存在

问题是,如果没有提供任何后端,pyjwt就不会设置默认后端。我已经厌倦了在我的Mac电脑上使用Python3.9的例子,它成功了

但是Python3.9不能在Raspberry Pi上工作,因为缺少一些依赖项。因此,我在Raspberry Pi上编译了Python 3.8.12,错误消失了

下面是编译Python的指南

https://raspberrytips.com/install-latest-python-raspberry-pi/

首先安装这些库,否则pip将无法工作,因为缺少ssl模块

sudo apt install libssl-dev
sudo apt install libncurses5-dev
sudo apt install libsqlite3-dev
sudo apt install libreadline-dev
sudo apt install libtk8.6
sudo apt install libgdm-dev
sudo apt install libdb4o-cil-dev
sudo apt install libpcap-dev

我不知道是否所有这些都是必要的,但它起了作用

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available

我在jwt.decode中遇到了同样的错误,但只在CI(linux)中出现了,并且它在我的mac上运行

检查python -m jwt.help您的加密版本是什么。pyjwt 2需要加密>;=3由于某种原因,我认为CI中是2.9.2,这可以解释为什么我认为它失败了

有一个commit更新了pyjwt[crypto]以要求有效的加密包,但由于某些原因,它没有显示在changelogs中,而且我通过手动将cryptography>=3.3.1,<4.0.0添加到requirements.txt中修复了这个问题

相关问题 更多 >

    热门问题