You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

OAuth2重定向URI无法触发ASWebAuthenticationSession回调,如何解决?

解决Discord OAuth2重定向URI不支持的问题

核心问题分析

你遇到的Redirect URI infinitea://auth is not supported by client错误,通常是重定向URI的一致性问题或URL编码缺失导致的,结合你的代码和配置,按以下步骤排查修复:

具体修复步骤

1. 确保Discord开发者平台的重定向URI完全匹配

  • 登录Discord开发者后台,进入你的应用→OAuth2→General页面,确认添加的重定向URI是精确的infinitea://auth,大小写、路径(/auth)都不能有偏差。
  • 添加后务必点击页面底部的Save Changes,很多人会忽略这一步导致配置不生效。

2. 对重定向URI进行URL编码

OAuth2要求redirect_uri参数必须经过URL编码,否则Discord会解析错误。修改你的authenticate函数:

public func authenticate(completion: @escaping (String?) -> Void) {
    // 对重定向URI进行URL编码
    guard let encodedRedirectUri = redirectUri.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
        completion(nil)
        return
    }
    // 使用编码后的URI拼接认证URL
    let authURL = URL(string: "https://discord.com/api/oauth2/authorize?client_id=\(clientId)&redirect_uri=\(encodedRedirectUri)&response_type=code&scope=\(scopes)")!
    print(authURL.absoluteString)
    
    let session = ASWebAuthenticationSession(url: authURL, callbackURLScheme: "infinitea") { url, error in
        if let url, let code = URLComponents(string: url.absoluteString)?.queryItems?.first(where: { $0.name == "code" })?.value {
            completion(code)
            return
        }
        completion(nil)
    }
    
    session.presentationContextProvider = self
    session.start()
}

注意:redirectUri变量的值必须是infinitea://auth,和Discord后台配置完全一致。

3. 完善Info.plist的URL Scheme配置

确保你的URL Scheme配置在Info.plist中是标准格式:

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>infinitea</string>
        </array>
        <key>CFBundleURLName</key>
        <string>com.mydomain.myproject</string>
    </dict>
</array>
  • 检查CFBundleURLSchemes数组中是否只有infinitea,不要添加多余字符;
  • CFBundleURLName可以是你的应用Bundle ID,确保唯一即可。

4. 验证ASWebAuthenticationSession的上下文提供器

确保你正确实现了ASWebAuthenticationPresentationContextProviding协议,让session能获取到正确的展示窗口:

extension YourAuthClass: ASWebAuthenticationPresentationContextProviding {
    func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
        // SwiftUI环境下获取当前窗口
        guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
              let window = windowScene.windows.first else {
            return ASPresentationAnchor()
        }
        return window
    }
}

如果上下文提供器返回无效窗口,可能导致session无法正常回调到应用。

5. 增强重定向回调日志排查

优化你的onOpenURL代码,增加日志便于排查:

.onOpenURL { url in
    print("Received redirect URL: \(url)")
    if url.scheme == "infinitea" {
        self.swiftCordAuth.handleIncomingURL(url)
    } else {
        print("Unknown URL scheme: \(url.scheme ?? "nil")")
    }
}

这样可以确认是否收到了Discord的重定向请求。

测试验证

  1. 清理Xcode缓存,重新编译应用;
  2. 触发认证流程,观察控制台输出的认证URL,确认redirect_uri参数是编码后的infinitea%3A%2F%2Fauth;
  3. 完成Discord登录后,网页应自动关闭并返回应用,控制台能打印出包含code的回调URL。

内容的提问来源于stack exchange,提问作者xTwisteDx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 04:25:23