为什么我在向Uber Sandbox发布请求时收到此错误? NSURLErrorDomain错误-1012

时间:2015-08-26 22:16:57

标签: ios swift uber-api

我可以从产品路径获取产品ID,但是当我尝试将请求发布到沙箱时,我会收到此错误。试图找出我在这里做错了什么。我添加了不记名令牌。我不确定它想要什么或者缺少什么。

Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x7f8c13939ed0 {NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/requests, NSUnderlyingError=0x7f8c0ac78410 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)", NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/requests}

func performPostUberRequest(url: NSURL, prodId: String) {
    let params:[String: AnyObject] = [
        "start_latitude" : "39.955715",
        "start_longitude" : "-75.1680298",
        "end_latitude" : "39.9542675",
        "end_longitude" : "-75.1409609",
        "product_id": prodId
    ]

    var error: NSError?
    var request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
    request.HTTPMethod = "POST"
    request.addValue("Bearer \(uberToken)", forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions.allZeros, error: &error)
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
        response, data, error in
        if let error = error {
            println(error)
        } else if data != nil {
            if let json: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? NSDictionary {
                println(json)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

根据您的评论:{ code = unauthorized; message = "Missing scope: request"; },您可能会错过范围,可以在您注册应用程序的Uber开发人员仪表板上指定范围,并获取秘密和客户端ID和内容。

如果您已完成此操作,则应在开始时请求authorization codeaccess tokenOAuth2.0时将必要的范围添加到参数中。

在我的imeplementation中,范围指定如下:

[[NXOAuth2AccountStore sharedStore] setClientID:_clientID
                                         secret:_clientSecret
                                          scope:[NSSet setWithObjects:@"request", @"history_lite", @"profile", @"request_receipt", nil]
                               authorizationURL:[NSURL URLWithString:@"https://login.uber.com/oauth/authorize"]
                                       tokenURL:[NSURL URLWithString:@"https://login.uber.com/oauth/token"]
                                    redirectURL:[NSURL URLWithString:_redirectURL]
                                  keyChainGroup:nil
                                 forAccountType:_applicationName];
相关问题