2016-08-02 55 views
2

我想发送使用swift和Alamofiretwilio API的请求,但我遇到了一些问题。AlamoFire for twilio

这是应该发送文本的功能:

func sendSMS() { 
    let parameters = [ 
     "To": "+12036044632", 
     "From": "+13852322267", 
     "Body": "Hi daddy" 
     ] 

    Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters).response { response in 
     print(response.0) 
     print(response.1) 
     print(response.2) 
     print(response.3) 
    } 
} 

当我运行它,我得到这个打印到控制台上:

`Optional(<NSMutableURLRequest: 0x7a844a30> { URL: https://AC11ed62fdb971a8f56d9be531a5ce40c2:[email protected]/2010-04-01/Accounts/AC11ed62fdb971a8f56d9be531a5ce40c2/Messages }) 
Optional(<NSHTTPURLResponse: 0x7a9f91f0> { URL: https://AC11ed62fdb971a8f56d9be531a5ce40c2:[email protected]/2010-04-01/Accounts/AC11ed62fdb971a8f56d9be531a5ce40c2/Messages } { status code: 401, headers { 
    "Access-Control-Allow-Credentials" = true; 
    "Access-Control-Allow-Headers" = "Accept, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since"; 
    "Access-Control-Allow-Methods" = "GET, POST, DELETE, OPTIONS"; 
    "Access-Control-Allow-Origin" = "*"; 
    "Access-Control-Expose-Headers" = ETag; 
    Connection = "keep-alive"; 
    "Content-Length" = 327; 
    "Content-Type" = "application/xml"; 
    Date = "Tue, 02 Aug 2016 02:21:01 GMT"; 
    "Twilio-Request-Duration" = "0.004"; 
    "Twilio-Request-Id" = RQ36001aed85114ea18c7eac4f20caaf59; 
    "Www-Authenticate" = "Basic realm=\"Twilio API\""; 
    "X-Powered-By" = "AT-5000"; 
    "X-Shenanigans" = none; 
} }) 

Optional(<3c3f786d 6c207665 7273696f 6e3d2731 2e302720 656e636f 64696e67 3d275554 462d3827 3f3e0a3c 5477696c 696f5265 73706f6e 73653e3c 52657374 45786365 7074696f 6e3e3c43 6f64653e 32303030 333c2f43 6f64653e 3c446574 61696c3e 596f7572 20416363 6f756e74 53696420 6f722041 75746854 6f6b656e 20776173 20696e63 6f727265 63742e3c 2f446574 61696c3e 3c4d6573 73616765 3e417574 68656e74 69636174 696f6e20 4572726f 72202d20 4e6f2063 72656465 6e746961 6c732070 726f7669 6465643c 2f4d6573 73616765 3e3c4d6f 7265496e 666f3e68 74747073 3a2f2f77 77772e74 77696c69 6f2e636f 6d2f646f 63732f65 72726f72 732f3230 3030333c 2f4d6f72 65496e66 6f3e3c53 74617475 733e3430 313c2f53 74617475 733e3c2f 52657374 45786365 7074696f 6e3e3c2f 5477696c 696f5265 73706f6e 73653e>) 
nil` 

然后我没有得到一个短信,并在Twilio控制台上,它不计数,不收取我的费用。我是否遇到某种错误?我究竟做错了什么?

注意:我了解在URL中进行身份验证的安全风险,并且这不是我正在寻找的修复程序,因此请不要发布或评论此问题。谢谢

回答

3

相反的:

Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters).response { response in 
    print(response.0) 
    print(response.1) 
    print(response.2) 
    print(response.3) 
} 

试试这个:

Alamofire.request(.POST, "https://MY SID:MY [email protected]/2010-04-01/Accounts/MY SID/Messages", parameters: parameters) 
    .authenticate(user: user, password: password) 
    .responseJSON { response in 
     let response = String(response.result.value) 
     print(response) 
} 

你^ h使用Alamofire正确验证Twilio,这就是“.authenticate”的意思。将变量用户更改为您的账户SID,并将变量密码更改为您的身份验证令牌。请参考https://www.twilio.com/console/account/settings查找您的帐户SID和身份验证令牌。

然后,您可以根据需要操纵响应字符串。

希望这会有所帮助。

3
{ status code: 401 

这意味着你没有通过身份验证。您的AccountSid或AuthToken可能不正确。如果你想检查你的证书,你可以先用curl从你的计算机上尝试,一旦你得到它的工作,你可以移动到Alamofire。

curl

curl -X POST 'https://api.twilio.com/2010-04-01/Accounts/ACc0966dd96e4d55d26ae72df4d6dc3494/Messages.json' \ 
--data-urlencode 'To=+16518675309' \ 
--data-urlencode 'From=+14158141829' \ 
--data-urlencode 'Body=Hey Jenny! Good luck on the bar exam!' \ 
-u ACc0966dd96e4d55d26ae72df4d6dc3494:[AuthToken] 

文档实例的请求在https://www.twilio.com/docs/api/rest/sending-messages

+0

刚刚尝试,它的工作,然后......但不与alamofire –

+0

如果您的凭据是好的,卷曲的作品,你可以尝试Alamofire但用http://而不是https://? –

+0

Babab仍然不能工作 –