尝试连接到API时出现OAuthSwift错误-3

时间:2019-04-11 23:19:59

标签: swift caching oauth token

我正在尝试使用OAuthSwift将OAuth 2与API结合使用。 也许我可以得到令牌原因,因为在AppDelegate中可以打印令牌URL(OAuthSwift.handle(url:url))

但是在View Controller中,我无法成功将令牌保存到钥匙串中。 我收到的错误是

Invalidating cache
The operation couldn’t be completed. (OAuthSwiftError error -3.)

我的代码:

let oauthswift = OAuth2Swift(
    consumerKey:    "5xxxxxxxxxxxxxxxxxxxxx",
    consumerSecret: "4xxxxxxxxxxxxxxxxxxxxx",
    authorizeUrl:    "https://api.orange.com/oauth/authorize",
    accessTokenUrl: "https://api.orange.com/oauth/token",
    responseType:   "code"
)

@IBAction func onTappedSyncButton(_ sender: UIButton) {
    let keychain = Keychain(service: "myapp-token")
    oauthswift.accessTokenBasicAuthentification = true

    if keychain["oauthToken"] == nil {
    let handle = oauthswift.authorize(
            withCallbackURL: URL(string: "my-orangesdk-app://orangehub")!,
            scope: "default",
            state:"",
            success: { credential, response, parameters in
                //Here, I couldn't print and save keychain
                print(credential.oauthToken)
                keychain["oauthToken"] = credential.oauthToken
            },
            failure: { error in
                print(error.localizedDescription)                  
            })
    }
}

AppDelegate:

func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
    if (url.host == "orangeapi") {
        OAuthSwift.handle(url: url)
        print(url)
        //Here, I could see token.
    }
    return true
}

0 个答案:

没有答案