我正在尝试使用 swift 和 Alamofire 发送对 twilio api 的发布请求,但我遇到了一些问题。
这是发送文本的函数:
func sendSMS() {
let parameters = [
"To": "+12036044632",
"From": "+13852322267",
"Body": "Hi daddy"
]
Alamofire.request(.POST, "https://MY SID:MY SECRET@api.twilio.com/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:UKCkDN1ojSeT4L27lc8uaNQ5qsvPJgMxd@api.twilio.com/2010-04-01/Accounts/AC11ed62fdb971a8f56d9be531a5ce40c2/Messages })
Optional(<NSHTTPURLResponse: 0x7a9f91f0> { URL: https://AC11ed62fdb971a8f56d9be531a5ce40c2:UKCkDN1oj2SeT4L7lc8uaNQ5qsvPJgMxd@api.twilio.com/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 控制台上,它不算数也不会向我收费。我会遇到某种错误吗?我做错了什么?
最佳答案
代替:
Alamofire.request(.POST, "https://MY SID:MY SECRET@api.twilio.com/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 SECRET@api.twilio.com/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)
}
您必须使用 Alamofire 正确验证进入 Twilio,这就是“.authenticate”的用途。将变量 user 更改为您的帐户 SID,并将变量 password 更改为您的身份验证 token 。引用 https://www.twilio.com/console/account/settings找到您的帐户 SID 和授权 token 。
然后您可以随意操作响应字符串。
希望这对您有所帮助。
关于ios - 适用于 twilio 的 AlamoFire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38710870/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我已经在Sinatra上创建了应用程序,它代表了一个简单的API。我想在生产和开发上进行部署。我想在部署时选择,是开发还是生产,一些方法的逻辑应该改变,这取决于部署类型。是否有任何想法,如何完成以及解决此问题的一些示例。例子:我有代码get'/api/test'doreturn"Itisdev"end但是在部署到生产环境之后我想在运行/api/test之后看到ItisPROD如何实现? 最佳答案 根据SinatraDocumentation:EnvironmentscanbesetthroughtheRACK_ENVenvironm
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
当我使用has_one时,它工作得很好,但在has_many上却不行。在这里您可以看到object_id不同,因为它运行了另一个SQL来再次获取它。ruby-1.9.2-p290:001>e=Employee.create(name:'rafael',active:false)ruby-1.9.2-p290:002>b=Badge.create(number:1,employee:e)ruby-1.9.2-p290:003>a=Address.create(street:"123MarketSt",city:"SanDiego",employee:e)ruby-1.9.2-p290
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
我正在使用带有Rails的Devise,我想添加一个方法“getAllComments”,所以我这样写:classUser在我的Controller中:defdashboard@user=current_user@comments=@user.getAllComments();end当我访问我的url时,我得到了undefinedmethod`getAllComments'for#我做错了什么?谢谢 最佳答案 因为getAllComments是一个类方法,而您正试图将其作为实例方法访问。您要么需要访问它:User.getAllCom
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
我正在使用Rails3.2.3和Ruby1.9.3p0。我发现我经常需要确定某个字符串是否出现在选项列表中。看来我可以使用Ruby数组.includemethod:或正则表达式equals-tildematchshorthand用竖线分隔选项:就性能而言,一个比另一个好吗?还有更好的方法吗? 最佳答案 总结:Array#include?包含String元素,在接受和拒绝输入时均胜出,对于您的示例只有三个可接受的值。对于要检查的更大的集合,看起来Set#include?和String元素可能会获胜。如何测试我们应该根据经验对此进行测试
Ruby初学者努力简单地将这个@@people散列的值打印到控制台classPerson#haveafirst_nameandlast_nameattributewithpublicaccessorsattr_accessor:first_nameattr_accessor:last_name#haveaclassattributecalled`people`thatholdsanarrayofobjects@@people=[]#havean`initialize`methodtoinitializeeachinstancedefinitialize(first_name,last_