我的应用程序需要登录basecamp3。因此,我使用了OAuth2Swift库。但不幸的是,即使用户已授权该应用程序,我也无法从basecamp接收令牌。

以下是屏幕截图ios - Swift中Oauth2的Basecamp3登录问题-LMLPHP

我使用了以下代码

    func createAuthRequest(){
    // create an instance and retain it
    let oauthswift = OAuth2Swift(
        consumerKey:    clientID,
        consumerSecret: clientSecret,
        authorizeUrl:   authURL,
        responseType:   "token"
    )

    //oauthswift.authorizeURLHandler = self
    oauthswift.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauthswift)

    let handle = oauthswift.authorize(
        withCallbackURL: URL(string: redirectURL)!,
        scope: "profile", state:"") { result in //This block of code never executed
            switch result {
            case .success(let (credential, response, parameters)):
                print(credential.oauthToken)
            // Do your request
            case .failure(let error):
                print(error.localizedDescription)
            }
    }
}


即使用户已授权该应用,withCallbackURL内部的代码也永远不会执行。

任何有关此的帮助表示赞赏。

最佳答案

我发现问题的解决方案是我使用了错误的身份验证和令牌URL。

需要使用以下URL。我错过了在auth / token url中添加web_server的权限,不幸的是Basecamp在他们的文档中没有提到相同的内容。

让authURL =“ https://launchpad.37signals.com/authorization/new?type=web_server

让tokenURL =“ https://launchpad.37signals.com/authorization/token?type=web_server

和redirectURL = com.abc.abc:/oauth2Callback(需要在basecamp开发者控制台下为该应用更新相同的redircturl,其中com.abc.abc是该应用的包ID)

10-08 15:34