ios - 为什么我在向 Uber Sandbox 发送请求时会收到此错误消息? NSURLErrorDomain 错误-1012

标签 ios swift uber-api

我可以从产品路径获取产品 ID,但在尝试向沙盒安排行程发布请求时出现此错误。试图弄清楚我在这里做错了什么。我添加了不记名 token 。我不确定它想要什么或缺少什么。

错误域=NSURLErrorDomain 代码=-1012“操作无法完成。(NSURLErrorDomain 错误-1012。)”UserInfo=0x7f8c13939ed0 {NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/requests,NSUnderlyingError=0x7f8c0ac78410 “操作无法完成。(kCFErrorDomainCFNetwork 错误 -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)
            }
        }
    }
}

最佳答案

根据您的评论:{ code = unauthorized;消息=“缺少范围:请求”; },您可能会错过范围,它可以在您的 Uber 开发人员仪表板上指定,您在其中注册了应用程序并获得了密码和客户端 ID 等。

如果您这样做了,您应该在通过 OAuth2.0 请求授权码访问 token 时将必要的范围添加到参数中> 在开头。

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

[[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];

关于ios - 为什么我在向 Uber Sandbox 发送请求时会收到此错误消息? NSURLErrorDomain 错误-1012,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32237618/

相关文章:

ios - 如何拉伸(stretch)PieChartView父View的整个宽度和长度

ios - 使用 Core Data 和 Magical Record 编辑自定义对象

ios - 防止 GMSMarker 在点击时重新定位相机 View

ios - 从 pickerview 中选定的行获取另一个值

ios - 在 iOS - Android MultiAutoCompleteTextView 中实现,使用 Whatsapp 中的自定义标记生成器 [附图]

uber-api - Uber API - 缺少范围 : request

ios - 在全宽 UIView 内水平对齐 UILabel 和 UIImageView

ios - 将异步 block 回调转换为同步返回值

java - Uber API - 提前预订

android - super 集成中的 Android 和 iOS 应用程序签名