Heimdall Swift 验证每个请求失败

Posted

技术标签:

【中文标题】Heimdall Swift 验证每个请求失败【英文标题】:Heimdall Swift authenticate each request failure 【发布时间】:2015-08-16 14:51:25 【问题描述】:

我正在使用 Heimdall https://github.com/rheinfabrik/Heimdall.swift 为 oauth2 访问和验证我自己的 api,但我似乎无法验证我的第二个请求。有没有人看到我做错的事情。我几乎可以肯定获取访问令牌的第一个请求是正确的。因为我最终进入了成功案例。

我的代码:

@IBAction func loginTapped(sender : AnyObject) 
    let username = InputUsername.text
    let password = InputPassword.text

    let tokenURL = NSURL(string: "https://[my-domain]/oauth/v2/token")!

    var identifier = "my-id"
    var secret = "my-secret"

    let credentials = OAuthClientCredentials(id: identifier, secret: secret)

    let heimdall = Heimdall(tokenURL: tokenURL, credentials: credentials)

    heimdall.requestAccessToken(username: username, password: password)  result in
        switch result 
        case .Success:
            self.callUserInfo(heimdall)
        case .Failure(let error):
            println("failure: \(error.description)")
        
    



func callUserInfo(heimdall: Heimdall) 
    let urlPath = "https://my-domain/auth/info"
    let url: NSURL = NSURL(string: urlPath)!

    let session = NSURLSession.sharedSession()
    var request = NSMutableURLRequest(URL: url)

    heimdall.authenticateRequest(request, completion:  result in
        switch result 
        case .Success:
            let task = session.dataTaskWithRequest(request)  data, response, error in
                let json = JSON(data)
                println(response.description)
            

            task.resume()
        case .Failure(let error):
            println("failure:")
        
    )

但结果是:

<NSHTTPURLResponse: 0x7fd82a265ab0>  URL: https://[my-domain]/auth/info   status code: 401, headers 
"Accept-Ranges" = bytes;
Age = 0;
"Cache-Control" = "no-store, private";
"Content-Length" = 78;
"Content-Type" = "application/json";
Date = "Sun, 16 Aug 2015 14:17:29 GMT";
Pragma = "no-cache";
Server = nginx;
Via = "1.1 varnish";
"Www-Authenticate" = "Bearer realm=\"Service\", error=\"access_denied\", error_description=\"OAuth2 authentication required\"";
"x-cache" = MISS;
"x-debug-token" = 6eea58;
"x-debug-token-link" = "/_profiler/6eea58";
"x-varnish" = 1051959467;
 

【问题讨论】:

【参考方案1】:

似乎是我自己的 API 对 Bearer 身份验证有不同的方法的问题

【讨论】:

以上是关于Heimdall Swift 验证每个请求失败的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 2 - 失败时的 Alamofire 打印响应值

swift 验证失败时,在 UITextField 中自动显示错误图标

异步等待失败 swift API 请求

在 swift 中使用 Alamofire 的 Json 请求失败

Keycloak CIBA 身份验证失败并显示“发送身份验证请求失败”

如何防止 ASP.NET 记录身份验证失败?