1

我的最终目标是一个带有开关的简单 iPhone 视图,当它“打开”时,会向我的 Spark Core(wifi 微芯片)发送一个发布请求以打开继电器。

我正在努力以适用于发布请求的格式添加参数。这在 CLI 中有效:

curl https://api.spark.io/v1/devices/<myDeviceId>/led -d access_token=<myAccessToken> -d params=l1,LOW

这是我尝试快速重现该请求:

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 ,其中键参数没有引号而值有引号。

谢谢!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~

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)&params=\(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
4

2 回答 2

1

Spark 要求您在请求正文而不是标头中发送访问令牌。

尝试以下操作:

var message = "access_token=<token>&params=<your_params>" 

然后在请求中:

request.HTTPBody = (message as NSString).dataUsingEncoding(NSUTF8StringEncoding)
于 2014-07-27T21:20:07.597 回答
0

根据我的经验,访问令牌需要是授权标头的一部分。试试这个

 request.setValue("access_token="\(YourAccessToken)", forHTTPHeaderField:"Authorization")
于 2014-07-27T19:54:00.950 回答