我在 python 中已经 1 周大了。 我从 python 调用 sudssoap api 。 这是我的代码。
logging.basicConfig(level=logging.INFO)
if __debug__:
logging.getLogger('suds.client').setLevel(logging.DEBUG)
else:
logging.getLogger('suds.client').setLevel(logging.CRITICAL)
client = Client(wsdlurl, soapheaders='string')
# Create ClientCustomerId parameter value for mutate method
clientId = client.factory.create("ClientCustomerId")
product = client.factory.create("Product")
clientId.product = product.INVITE_ADVERTISER
clientId.entityId = XXXX
# Create UserListOperation parameter value for mutate method
# Create UserList Object
userList = client.factory.create("UserList")
millis = int(round(time.time() * 1000))
userList.name = "Demographic segment # %s" % millis
userList.description = "Target dempographic description"
userList.integrationCode = "XXX"
d = timedelta(days=365)
userList.membershipLifeSpan = d.days
userListOperation = client.factory.create("UserListOperation")
userListOperation.operand = userList
operator = client.factory.create("Operator")
userListOperation.operator = operator.ADD
print userListOperation
operations = [userListOperation]
result = client.service.mutate(clientId, userListOperation) //Error raises here
print result
在调用 mutate() 方法时出现错误,如服务器引发错误:'[AuthenticationError.LOGIN_COOKIE_REQUIRED @ ;触发器:'']'
在客户端方法中,我添加了 SOAP 头,例如 client = Client(wsdlurl,soapheaders='string'),否则我会收到错误“缺少 SOAP 头”
由于登录 cookie 错误,尝试传递访问 token 但没有任何效果。 client = Client(wsdlurl,headers{'key':'value'},soapheaders='string')#,soapheaders{'userAgent':'administrator'})
#atoken = Element('token').setText(token)
#locale = Element('locale').setText('en')
#client.set_options(soapheaders=(atoken,locale))
浏览了很多,但似乎没有什么用处。 有什么帮助吗??
最佳答案
终于我找到了解决方案,可能对任何初学者都有用。
使用 suds 模块调用 SOAP API 时,我们需要设置 header ,在我的例子中,我通过 OAuth 身份验证传递访问 token ,如下所示
code='Bearer %s'%accToken
headers={'Authorization':code}
client = Client(wsdlurl,headers=headers,soapheaders=(appname))
这里 accToken 是我的访问 token ,headers 是一个字典,我传递的键是“Authorization”,值是“Bearer XXX(accToken)”,appname 是我的应用程序名称(只是一个字符串)
干杯
关于python - 在 python 中调用 SOAP 客户端(suds) 返回错误 AuthenticationError.LOGIN_COOKIE_REQUIRED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36352790/