获取“无效的\u令牌\u格式安全令牌格式不符合预期架构。”docusign legacy auth head

2024-09-26 22:08:57 发布

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

我正在尝试使用Python请求编写一个请求,它将请求发送到Docusign。我需要使用legacy authorization头文件,但不幸的是,似乎大多数用于此的文档都已被删除。当我发送请求时,我得到了标题中所述的错误。你知道吗

通过研究,我发现密码中的特殊字符会导致这个问题,所以我确认我的密码没有特殊字符,并且我的API密钥是正确的。我目前发送的标题作为一个字符串字典如下所示。我尝试过其他几种方法,这似乎是最接近的,但仍然会导致错误。我尝试过的其他方法包括尝试将标题写成单个字符串(而不是先形成一个字典),但这似乎没有更好的效果。你知道吗

docusign_auth_string = {}
docusign_auth_string["Username"] = docusign_user
docusign_auth_string["Password"] = docusign_password
docusign_auth_string["IntegratorKey"] = docusign_key

docusign_auth_string = str(docusign_auth_string)

headers = {'X-DocuSign-Authentication': docusign_auth_string}

response = requests.post(docusign_url, headers=headers, data=body_data)

上面的代码返回一个401,消息为INVALID \u TOKEN \u FORMAT“安全令牌格式不符合预期的架构”。我发送的头如下所示: {'X-DocuSign-Authentication':“{'Username':'test@test.com“,”Password“:”xxxxxxxxx“,”IntegratorKey“:”xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}“}

当我通过邮递员发送请求时,效果很好。在Postman中,我输入标题名为X-Docusign-Authentication,值为:{“Username”:“{ds\u Username}”,“Password”:“{ds\u Password}”,“IntegratorKey”:“{ds\u integrator\u key}”}(与python代码中的变量值相同)。你知道吗

因此,它肯定与请求发送头的方式有关。你知道吗

有人知道我为什么会出现上述错误吗?你知道吗


Tags: auth密码标题stringauthentication错误dsusername
3条回答

由于您成功地使用了Postman,因此通过您的请求可以准确地获得所发送的内容。对于此用途:

response = requests.get(your_url, headers=your_headers)
x = response.request.headers()
print(x)

这将向您显示正在准备和发送的请求。如果你在这里张贴的答复,我很乐意帮助更多。你知道吗

How can I see the entire HTTP request that's being sent by my Python application?

第二个答案显示了响应对象的所有可能参数。你知道吗

我找到了解决这个问题的办法。提到双引号的响应是正确的,但在Python中,我无法发送具有正确格式的字符串,以便docusign理解。接下来,我发现了以下堆栈溢出问题,它最终提供了解决方案:

How to send dict in Header as value to key 'Authorization' in python requests?

我曾经json.dumps文件这就解决了问题。我的代码如下:

docusign_auth_string = {}
docusign_auth_string["Username"] = docusign_user
docusign_auth_string["Password"] = docusign_password
docusign_auth_string["IntegratorKey"] = docusign_key

headers = {"X-DocuSign-Authentication": json.dumps(docusign_auth_string), "Content-Type": "application/json"}

我能够重现这种行为:看起来DocuSign不接受x-DocuSign-Authentication头值的子参数周围的单引号。你知道吗

你的例子失败了:

{'Username': 'test@test.com', 'Password': 'xxxxxxxxxx', 'IntegratorKey': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'}

这更成功:

{"Username": "test@test.com", "Password": "xxxxxxxxxx", "IntegratorKey": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}

我对Python还不太熟悉,不知道是否有其他代码结构可以使用双引号而不是单引号。在最坏的情况下,您可能需要手动设置标题值以遵循该格式。你知道吗

相关问题 更多 >

    热门问题