huawei api sms文档

时间:2014-03-21 14:46:16

标签: api sms

是否有人拥有华为SMS API文档? (API / SMS / SMS-列表)

我需要知道如何与此API交谈以获取短信列表:

必须是这样的:

<request>
<PageIndex>1</PageIndex>
<ReadCount>20</ReadCount>
<BoxType>1</BoxType>
<SortType>0</SortType>
<Ascending>0</Ascending>
<UnreadPreferred>0</UnreadPreferred>
</request>

但我只得到一个错误代码100003作为答案。我不是那个意思。

谢谢你, 米歇尔

3 个答案:

答案 0 :(得分:1)

我在使用Python的华为E5221上完成了这项工作。您收到的错误是因为您未经过身份验证,需要先登录。然后可以检索列表。

另请注意,所有API请求都是POST而不是GET。

登录方法:

def LoginToSMSGateway(sms_gateway_ip, username, password):
api_url = '/api/user/login'
post_data = '<request><Username>'+username+'</Username><Password>'+ base64.b64encode(password) +'</Password>'

r = requests.post(url='http://' + sms_gateway_ip + api_url, data=post_data)
if r.status_code == 200:
    result = False
    root = ET.fromstring(r.text)

    for results in root.iter('response'):
        if results.text == 'OK':
            result = True

    return result
else:
    return False

检索SMS List的方法(该方法将XML结果转换为SMS的Python列表):

def GetSMSList(sms_gateway_ip):
class SMS:
    Opened = False
    Message = ''


api_url = '/api/sms/sms-list'
post_data = '<?xml version="1.0" encoding="UTF-8"?><request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>0</UnreadPreferred></request>'

headers = {'Referer': 'http://' + sms_gateway_ip + '/html/smsinbox.html'}

r = requests.post(url='http://' + sms_gateway_ip + api_url, data=post_data, headers=headers)

root = ET.fromstring(r.text)

resultsList = list()
for messages in root.iter('Messages'):
    for message in messages:
        sms = SMS()
        sms.Message = message.find('Content').text
        sms.Opened = False if message.find('SmsType').text == '1' else True
        resultsList.append(sms)

return resultsList

使用它(IP和凭证是默认值,需要加以保护。):

if LoginToSMSGateway('192.168.1.1', 'admin', 'admin'):
print 'Logged in.'

smsList = GetSMSList('192.168.1.1')
for sms in smsList:
    print sms.Message

答案 1 :(得分:1)

由于此主题已发布,我认为API已经发生了一些变化。

要获取SMS列表,无需登录,但您需要获取sessionID和相应的令牌。您可以使用该方法来获取它们。

def GetTokenAndSessionID(sms_gateway_ip):
    url = '/html/smsinbox.html'
    r = requests.get(sms_gateway_ip + url)
    Setcookie = r.headers.get('set-cookie')
    sessionID = Setcookie.split(';')[0]
    token =  re.findall(r'"([^"]*)"', r.text)[2]
    return token, sessionID

然后获取sms列表的方法(它使用第一个方法将sessionID和Token包装在头文件中)。

def GetSmsList(sms_gateway_ip):
    class SMS:
        Opened = False
        Message = ''

    url = '/api/sms/sms-list'
    token,sessionID = GetTokenAndSessionID(sms_gateway_ip)
    post_data = '<request><PageIndex>1</PageIndex><ReadCount>20</ReadCount><BoxType>2</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>0</UnreadPreferred></request>'
    headers = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                '__RequestVerificationToken': token,
                'Cookie': sessionID
              }
    r = requests.post(sms_gateway_ip + url, data = post_data, headers=headers)

    root = ET.fromstring(r.text)
    resultsList = list()

    for messages in root.iter('Messages'):
        for message in messages :
            sms = SMS()
            sms.Message = message.find('Content').text
            sms.Opened = False if message.find('SmsType').text == '1' else True
            resultsList.append(sms)

要使用它,您只需要使用sms网关的ip调用它:192.168.8.1

GetSmsList(sms_gateway_ip)

答案 2 :(得分:0)

使用Apache HTTP Client类的Java中的类似代码

CloseableHttpClient httpclient = HttpClients.createDefault();

// 1. Have apache HTTPClient manage the cookie containing the SessionID
CookieStore cookieStore = new BasicCookieStore();
HttpContext httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

// 2. Extract the token
String token = "";
HttpGet hget = new HttpGet("http://192.168.8.1/html/smsinbox.html");
CloseableHttpResponse getRespo = httpclient.execute(hget, httpContext);
try {
    StatusLine statusLine = getRespo.getStatusLine();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = getRespo.getEntity();
    if (entity == null) {
        throw new ClientProtocolException("Get response contains no content");
    }
    long len = entity.getContentLength();
    StringTokenizer st = null;
    if (len != -1 && len > 250) {
        st = new StringTokenizer(EntityUtils.toString(entity).substring(0, 250), "\"");
    }
    int i = 1;
    while (st != null && st.hasMoreTokens()) {
        if (i++ == 10) {
            token = st.nextToken();
            break;
        } else {
            st.nextToken();
        }
    }
} finally {
    getRespo.close();
}
System.out.println("Token: " + token);

// 3. Get the SMS messages using the Token
HttpPost hpost = new HttpPost("http://192.168.8.1/api/sms/sms-list");

String xmlRequest = "<request><PageIndex>1</PageIndex><ReadCount>1</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>";
StringEntity reqEntity = new StringEntity(xmlRequest);
reqEntity.setContentType("text/xml");

hpost.setEntity(reqEntity);
hpost.addHeader("__RequestVerificationToken", token);

CloseableHttpResponse postRespo = httpclient.execute(hpost, httpContext);
try {
    StatusLine statusLine = postRespo.getStatusLine();
    if (statusLine.getStatusCode() >= 300) {
        throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
    }
    HttpEntity entity = postRespo.getEntity();
    if (entity == null) {
        throw new ClientProtocolException("Response contains no content");
    }
    System.out.println(EntityUtils.toString(entity));
    //Your further processing here. 
} finally {
    postRespo.close();
}