获取"INVALID_TOKEN_FORMAT The security token format does not conform to expected schema."文档签名旧版身份验证标头



我正在尝试使用Python Requests编写一个请求,该请求向Docusign发送请求。 我需要使用旧版授权标头,但不幸的是,似乎大多数文档已被删除。 当我发送请求时,我收到标题中所述的错误。

通过研究,我发现密码中的特殊字符可能会导致此问题,因此我确认我的密码没有特殊字符,并且我的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_TOKEN_FORMAT"安全令牌格式不符合预期的架构"。 我发送的标头如下所示: {'X-DocuSign-Authentication': "{'用户名

': 'test@test.com', '密码': 'xxxxx', 'IntegratorKey': 'xxxx-xx-xxxx-xx-xx当我通过邮递员发送请求时,它工作得很好。 在Postman中,我将标头名称输入为X-Docusign-Authentication,值为:{"用户名":"{{ds_username}}","密码":"{{ds_password}}","IntegratorKey":"{{ds_integrator_key}}"}(替换与python代码中相同的变量值)。

因此,它肯定与请求发送标头的方式有关。

有谁知道为什么我可能会收到上述错误?

我能够重现这种行为:看起来 DocuSign 不接受 x-DocuSign-身份验证标头值的子参数周围的单引号。

您的示例失败:

{'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 不够熟悉,无法建议您是否可以遵循不同的代码结构来使用双引号而不是单引号。最坏的情况是,您可能需要手动设置标头值以遵循该格式。

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

如何将标头中的字典作为值发送到 python 请求中的键"授权"?

我使用了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"}

由于您成功地使用Postman,因此它将有助于准确获取通过您的请求发送的内容。对于此用途:

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

这将准确显示正在准备和发送的请求。如果您在此处发布该回复,我很乐意为您提供更多帮助。

如何查看我的 Python 应用程序发送的整个 HTTP 请求?

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

相关内容

最新更新