我对SoftLayer_Ticket有一些疑问:
SoftLayer_Ticket :: createStandardTicket
我对参数感到困惑:http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/createStandardTicket。
这个api的一些参数(attachmentId,rootPassword ...)是可选的,但我找不到省略它们的方法。
我想我可以使用“paramName = XXX”来指定我需要的参数,并调用如下:
client['Ticket'].createStandardTicket(templateObject=templateObject,contents=contents, attachedFiles=attachedFiles)
但它失败了。如果我想附加一些文件而不是服务器,我该如何调用这个函数?请举个例子,谢谢〜
SoftLayer_Ticket :: addAttachedFile
我想通过此API附加一些文件:
test.txt(文本文件)和test.png(图片)
我成功附加了test.txt,但未能附上test.png。
代码:
import SoftLayer
from SoftLayer import utils
class Ticket_Manager(object):
def __init__(self, client):
self.sl_account = client['Account']
self.sl_ticket = client['Ticket']
self.sl_ticket_subject = client['Ticket_Subject']
def test_attach_files(self, ticket_config):
if ticket_config.has_key('attachedFiles'):
for file in ticket_config['attachedFiles']:
print self.sl_ticket.addAttachedFile(file, id=27333259)
def test_create_ticket(self, ticket_config):
pass
if __name__ == '__main__':
client = SoftLayer.create_client_from_env(username=API_USERNAME,api_key=API_KEY)
ticket_mgt = Ticket_Manager(client)
ticket_contents = 'SoftLayer API(SoftLayer_Ticket) test, thanks!'
with open('test.txt', 'rb') as file1:
bytes_stream1 = file1.read()
file1.close()
with open('test.png', 'rb') as file2:
bytes_stream2 = file2.read()
file2.close()
ticket_config = {
'subjectId':1522,
'title':'SoftLayer API test(no need to reply)',
'contents':ticket_contents,
'serverId':0,
'serverRootPassword':'',
'accessPort':'',
'serverType':'virtual',
'type':'standard',
'attachedFiles':[
{
'filename':'test.txt',
'data':bytes_stream1
},
{
'filename':'test.png',
'data':bytes_stream2
}
],
'attachedAdditionalEmails':[
]
}
ticket_mgt.test_attach_files(ticket_config)
我在linux下运行这个脚本,遇到了这个例外:
SoftLayer.exceptions.SoftLayerAPIError:SoftLayerAPIError(SoftLayer_Ticket):找不到有效的身份验证标头。
答案 0 :(得分:0)
问题1:
必须传递所有参数,以便您可以避免任何问题,但您可以将它们设置为空。
尝试此示例附加文件,它工作正常:
"""
Create Standard Ticket
This script creates a standard support ticket with file attached
Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Ticket/createStandardTicket
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket
http://sldn.softlayer.com/reference/datatypes/SoftLayer_Ticket/createStandardTicket_File_Attachment
License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer
# Define your SoftLayer username and apiKey
USERNAME = 'set me'
API_KEY = 'set me'
# Declare the API client
client = SoftLayer.Client(username=USERNAME, api_key=API_KEY)
# Build a SoftLayer_Ticket object
templateObject = {
"assignedUserId": 123123,
"subjectId": 1522,
"title": "ticket example"
}
# Define contents
contents = 'this the ticket content'
# Define the name of a file that is uploaded to the SoftLayer API.
filename = "TestPicture.jpg"
# Reading file
with open("C:/Users/Ruber Cuellar/Pictures/allow.jpg", "rb") as f:
byte = f.read()
f.close()
# Build SoftLayer_Container_Utility_File_Attachment object
attachedFiles = [
{
"filename": filename,
"data": byte
}
]
# Define additional parameters
attachId = 0
rootPassword = ""
controlPanelPassword = ""
accesPort = ""
try:
result = client['SoftLayer_Ticket'].createStandardTicket(templateObject, contents, attachId, rootPassword, controlPanelPassword, accesPort, attachedFiles)
print (result)
except SoftLayer.SoftLayerAPIError as e:
print("Unable to create standard ticket. "
% (e.faultCode, e.faultString))
问题2:
正如我所见,您正在使用此方法:addAttachedAdditionalEmails,如果要附加文件,则需要使用:SoftLayer_Ticket::addAttachedFile方法,如果需要,它只允许附加单个文件要在一次调用中附加多个文件,请尝试使用此方法:SoftLayer_Ticket::addUpdate
另外,例外:
SoftLayer.exceptions.SoftLayerAPIError: SoftLayerAPIError(SoftLayer_Ticket):没有有效的身份验证标头 找到。
这是与身份验证相关的内容,如果您可以提供您正在尝试的代码,那么确定可能存在的问题会很有帮助
更新
我查看并验证了您的脚本是否成功运行。但是,报道了一个问题:
https://github.com/softlayer/softlayer-python/issues/506
看一下它的解决方案: