使用DocuSign API获取月度信封时遇类型错误求助
解决DocuSign API调用中的"Unsupported operand type(s) for +: 'Configuration' and 'str'"错误
问题根源
你代码里的身份验证配置完全不符合DocuSign Python SDK的要求——configuration.api_key被赋值为字典,但该字段要求是字符串(或字符串列表),导致SDK内部处理请求时错误地尝试将Configuration对象和字符串拼接,触发类型异常。另外,旧版的XML格式认证头不能直接塞给api_key字段。
修正代码
from docusign_esign import Configuration, ApiClient, EnvelopesApi from datetime import datetime, timedelta import calendar # 初始化配置 config = Configuration() config.host = "https://demo.docusign.net/restapi" # 正确配置认证:先设置集成密钥,再添加认证头 config.api_key = [integrator_key] api_client = ApiClient(config) api_client.set_default_header( "X-DocuSign-Authentication", f'<DocuSignCredentials><Username>{api_username}</Username><Password>{api_password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>' ) envelopes_api = EnvelopesApi(api_client) try: profile_id = input("Enter profile ID : ") now = datetime.now() # 简化上月日期计算 first_day_last_month = datetime(now.year, now.month - 1, 1) last_day_last_month = datetime(now.year, now.month - 1, calendar.monthrange(now.year, now.month - 1)[1]) # 调用API,用isoformat直接生成符合要求的日期格式 results = envelopes_api.list_status_changes( account_id=profile_id, from_date=first_day_last_month.isoformat(), to_date=last_day_last_month.isoformat() ) print("上月发送的信封:") for env in results.envelopes: print(f"ID: {env.envelope_id}, 状态: {env.status}, 创建时间: {env.created_datetime}") except Exception as e: print(f"错误信息: {e}")
核心修正点
- 修复认证配置:
config.api_key必须设为你的集成密钥(列表格式是SDK强制要求)- 单独通过
api_client.set_default_header添加X-DocuSign-Authentication头,不能直接赋值给api_key
- 简化日期逻辑:直接构造上月的起止日期,避免冗余的时间操作
- 规范日期格式:用
isoformat()生成标准ISO 8601字符串,无需手动拼接格式
额外提示
- 生产环境禁止使用用户名密码认证,改用JWT认证更安全合规
- 确保
api_username、api_password、integrator_key三个变量已正确赋值,不要留空
内容的提问来源于stack exchange,提问作者Support VyGeek
相关产品推荐
相关产品推荐

