我的最终目标是一个简单的iPhone视图,带有一个开关,当打开时,向我的Spark Core(wifi微芯片)发送一个发布请求以打开一个继电器。
我正在努力以一种适用于post请求的格式添加params。这适用于CLI:
curl https://api.spark.io/v1/devices/<myDeviceId>/led -d access_token=<myAccessToken> -d params=l1,LOW
这是我尝试在swift中重现请求:
func toggleLight (on: Bool){
var urlToUse = sparkAPIBaseURL+coreId+lightsMethodName
var url = NSURL.URLWithString(urlToUse)
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
if (on==true) {
request.setValue(paramsForOn, forHTTPHeaderField: "params")
} else {
request.setValue(paramsForOff, forHTTPHeaderField: "params")
}//if
request.setValue(accessToken, forHTTPHeaderField: "access_token")
println("request: \(request)")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in println("response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(strData)
self.responseDataLabel.text = strData
})//task
task.resume() //no idea what this does
}//toggleLight
println显示请求在密钥“access_token”周围有无关的引号,我认为这是问题所在:
request: <NSMutableURLRequest: 0x7a62a450> { URL: https://api.spark.io/v1/devices/<myDeviceId>/led, headers: {
"access_token" = <myAccessToken>;
params = "l1,LOW"; } }
响应显示未找到访问令牌的错误:
response: <NSHTTPURLResponse: 0x7b654c60> { URL: https://api.spark.io/v1/devices/<myDeviceId>/led } { status code: 400, headers {
"Access-Control-Allow-Origin" = "*";
Connection = "keep-alive";
"Content-Length" = 104;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sun, 27 Jul 2014 18:21:45 GMT";
Server = "nginx/1.6.0";
"X-Powered-By" = Express;} }{
"code": 400,
"error": "invalid_request",
"error_description": "The access token was not found"}
我尝试了setValue以及addValue,但他们似乎都通过添加引号来处理密钥access_token,并且没有引号的值,其中key params没有引号且值为。
谢谢!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
Stack不会让我回答我自己的问题几个小时,所以这是解决问题的工作代码: 感谢Jonah - 我从朋友那里了解到Spark Core需要身体中的键值对,所以这是让我超越驼峰的工作代码:
func toggleLight (on: Bool){
var urlToUse = sparkAPIBaseURL+coreId+lightsMethodName
var url = NSURL.URLWithString(urlToUse)
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
var params: String
if (on==true) {
params = paramsForOn
} else {
params = paramsForOff
}//if
**var message = "access_token=\(accessToken)¶ms=\(params)"
request.HTTPBody = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding)**
println("request: \(request)")
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in println("response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(strData)
self.responseDataLabel.text = strData
})//task
task.resume() //no idea what this does
}//toggleLight
答案 0 :(得分:1)
Spark要求您在请求正文中而不是标题中发送访问令牌。
尝试以下方法:
var message = "access_token=<token>¶ms=<your_params>"
然后在请求中:
request.HTTPBody = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding)
答案 1 :(得分:0)
根据我的经验,访问令牌需要是授权标头的一部分。试试这个
request.setValue("access_token="\(YourAccessToken)", forHTTPHeaderField:"Authorization")