Heimdallr.swift无法使用访问令牌

Heimdallr.swift无法使用访问令牌,swift,oauth,oauth-2.0,swift2,Swift,Oauth,Oauth 2.0,Swift2,我正在使用swift应用程序中的Heimdallr.swift存储库使用OAuth2密码授权登录。但在收到“成功”消息后,我仍然无法访问受保护的资源。是否有人知道如何保存您收到的令牌,或者问题是什么 @IBAction func loginButton(sender: UIButton) { let username: String = usernameTextfield.text!; let password: String = passwordTextfield.text!

我正在使用swift应用程序中的Heimdallr.swift存储库使用OAuth2密码授权登录。但在收到“成功”消息后,我仍然无法访问受保护的资源。是否有人知道如何保存您收到的令牌,或者问题是什么

@IBAction func loginButton(sender: UIButton) {

    let username: String = usernameTextfield.text!;
    let password: String = passwordTextfield.text!;

    let tokenURL = NSURL(string: "http://challyme.dk:8888/index.php/api/v1.1/oauth/access_token")!

    let identifier = "id0"
    let secret = "secret0"

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

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

    heimdall.requestAccessToken(username: username, password: password) { result in
        switch result {
        case .Success:
            self.callUserInfo(heimdall)
            dispatch_async(dispatch_get_main_queue()) {
                let mainStoryboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
                let vc : UIViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LogedInView") as UIViewController
                self.presentViewController(vc, animated: true, completion: nil)
            }
        case .Failure:
            dispatch_async(dispatch_get_main_queue()) {
            print("Wrong password or username")
            let alertView = UIAlertController(title: "Alert", message: "You entered the wrong username or password", preferredStyle: .Alert)
            alertView.addAction(UIAlertAction(title: "Ok", style: .Default, handler: nil))
            self.presentViewController(alertView, animated: true, completion: nil)
            }
        }
    }

}
  • 应显示受保护资源的方法:

    func callUserInfo(heimdall: Heimdallr) {
          let urlPath = "http://linkToResources"
          let url: NSURL = NSURL(string: urlPath)!
    
          let session = NSURLSession.sharedSession()
          let request = NSMutableURLRequest(URL: url)
    
          heimdall.authenticateRequest(request, completion: { result in
            switch result {
            case .Success(let request):
                let task = session.dataTaskWithRequest(request) { data, response, error in
                    let json = JSON(data: data!);
                    print(response!.description);
                    if response != nil {
                         print(json);
                     } else {
                        print(json[999].error) // "Array[999] is out of bounds"
                     }
    
                 }
                task.resume()
    
            case .Failure:
                print("failure")
            }
        })
    }
    
致意
Anders B.Christensen

晚了一点,但你能解决你的问题吗?晚了一点,但你能解决你的问题吗?