1

我正在使用 OAuthSwift 将用户发送到 Twitch 以授权我的应用程序。但是,一旦应用程序获得授权,用户就不会被送回我的应用程序。我收到一条消息,出现在 Safari 中:

You are about to leave Twitch. Twitch has no control over the content or security of http://localhost.

我试图在 AppDelegate 中处理这个问题,但它似乎没有重定向:

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey  : Any] = [:]) -> Bool {
    if (url.host == "http://localhost") {
        OAuthSwift.handle(url: url)
    }
    return true
}

这是触发 Twitch 身份验证的代码:

@IBAction func didPressLoginButton(_ sender: Any) {

    // create an instance and retain it

        let oauthswift = OAuth2Swift(
            consumerKey:    "xxxxx",
            consumerSecret: "xxxxx",
            authorizeUrl: "https://id.twitch.tv/oauth2/authorize",
            accessTokenUrl: "https://id.twitch.tv/oauth2/token",
            responseType:   "code",
            contentType:    "application/json"
        )

        oauthswift.authorizeURLHandler = OAuthSwiftOpenURLExternally.sharedInstance

        self.oauthswift = oauthswift

        //oauthswift.accessTokenBasicAuthentification = true
        //let codeVerifier = base64url("abcd...")
        //let codeChallenge = codeChallenge(for: codeVerifier)

        let handle = oauthswift.authorize(withCallbackURL: URL(string: "http://localhost")!, scope: "user:read:email", state:"TWITCH") { result in

            switch result {

                case .success(let (credential, response, parameters)):
                    print(credential.oauthToken)
                    TwitchTokenManager.shared.accessToken = credential.oauthToken
                    //Twitch.Clips.getClips

                // Do your request

                case .failure(let error):
                    print(error.localizedDescription)
            }
        }
    }

任何帮助将不胜感激。谢谢 :)

4

1 回答 1

0

我在 Android 中说过这个(不确定它是否会有所帮助),但事实证明,我在 webview 中输入了错误的客户端 ID。

一旦我将其更改为 Twitch 开发仪表板中的正确客户端 ID,它就会打开身份验证页面。另外,我使用我的家庭 URL 作为重定向,而不是 localhost。也不确定这是否有所作为。祝你好运!!

于 2020-04-17T21:04:31.513 回答