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

SwiftUI应用集成Facebook登录遇Graph API请求错误求助

SwiftUI Facebook登录:Graph API请求错误-1(TestFlight/模拟器均出现)

我在SwiftUI应用中集成Facebook登录功能,无论是通过TestFlight安装到真机,还是在模拟器测试时,都遇到了错误:Error: The operation could not be completed. (Could not make graph API request error -1)。登录流程能走到“登录成功”的打印,但后续调用Graph API获取用户数据时失败。

相关登录函数代码:

func loginWithFacebook() {
        let loginManager = LoginManager()
        
        loginManager.logOut()

        loginManager.logIn(permissions: ["public_profile"], from: nil) { result, error in
            if let error = error {
//                showAlert(msg: "Facebook login failed with error: \(error.localizedDescription)")
                print("Facebook login failed with error: \(error.localizedDescription)")
                // Handle login failure
            } else if result?.isCancelled == true {
//                showAlert(msg: "Facebook login was cancelled")
                print("Facebook login was cancelled")
                // Handle login cancellation
            } else {
                print("Facebook login successful")
                
                guard let token = AccessToken.current else { return }
                
                if token.hasGranted(permission: "public_profile") {
                    // Perform a Graph API request to get user's profile data
                    let graphRequest = GraphRequest(graphPath: "me", parameters: ["fields": "id,name"], tokenString: token.tokenString, version: nil, httpMethod: .get)

                    graphRequest.start { _, result, error in
                        if let error = error {
                            showAlert(msg: "Failed to fetch Facebook user data: \(error.localizedDescription)")
                            print("Failed to fetch Facebook user data: \(error.localizedDescription)")
                            // Handle the error
                        } else if let userData = result as? [String: Any] {
                            // Extract user information
                            if let userID = userData["id"] as? String,
                               let userName = userData["name"] as? String {
                                print("User ID: \(userID)")
                                print("User Name: \(userName)")

                                // Perform additional actions with the user data
                                otherLogingType = "facebook"
                                submitOtherLogin(username: userName, userEmail: "", userId: userID)
//                                showAlert(msg: "No issue with Facebook, check other login API")
                            }
                        }
                    }
                }
            }
        }
    }

可能的解决方案

  • 检查Facebook应用平台配置

    1. 确认Facebook开发者后台中,iOS平台的Bundle ID和应用的开发/TestFlight分发Bundle ID完全一致。
    2. 检查Server IP Allowlist是否开启了不必要的IP限制,导致Graph API请求被拦截。
    3. 确保iOS平台的Single Sign On选项已启用。
  • 显式指定Graph API版本
    创建GraphRequest时未指定版本会使用SDK默认旧版本,可能存在兼容性问题,建议显式指定当前可用的最新版本:

    let graphRequest = GraphRequest(graphPath: "me", 
                                   parameters: ["fields": "id,name"], 
                                   tokenString: token.tokenString, 
                                   version: "v18.0", // 替换为当前最新版本
                                   httpMethod: .get)
    
  • 网络ATS配置检查
    在Info.plist中配置正确的App Transport Security规则,确保允许Facebook域名的HTTPS请求:

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>facebook.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
            </dict>
            <key>fbcdn.net</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
            </dict>
        </dict>
    </dict>
    
  • AccessToken有效性验证
    调用Graph API前,确保Token未过期且有效:

    guard let token = AccessToken.current, !token.isExpired else {
        print("Facebook token is invalid or expired")
        return
    }
    
  • SDK版本与能力配置

    1. 更新Facebook SDK到最新版本,确保与当前Xcode、iOS版本兼容。
    2. 检查应用Signing & Capabilities中,已添加fb<你的APP_ID>格式的URL Scheme。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 03:00:58