1

我一直在尝试使用此代码来获取我的 iOS 应用程序所需的用户信息,当我按下 func loginButton 时,我应该登录并获取用户名和电子邮件,

    func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {

    if( error != nil)
    {
        println(error.localizedDescription)
        return
    }

    if let userToken = result.token{
        //Get user acces token 
        let token:FBSDKAccessToken=result.token

        println("Token = \(FBSDKAccessToken.currentAccessToken().tokenString)")

        println("User ID = \(FBSDKAccessToken.currentAccessToken().userID)")


        let protectedPage = self.storyboard?.instantiateViewControllerWithIdentifier("ProtectedPageViewController") as! ProtectedPageViewController
        let protectedPageNav = UINavigationController(rootViewController: protectedPage)

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

        appDelegate.window?.rootViewController = protectedPageNav

    }

    //Show user information
    let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)

    graphRequest.startWithCompletionHandler({ (connection, result, error) -> Void in

        if ((error) != nil)
        {
            // Process error
            println("Error: \(error)")
        }
        else
        {
            println("fetched user: \(result)")
            let userEmail : NSString = result.valueForKey("email") as! NSString
            println("User Email is: \(userEmail)")
            let userName : NSString = result.valueForKey("name") as! NSString
            println("User Name is: \(userName)")

        }
    })
}

有一条评论显示用户信息,它应该打印电子邮件和用户名,它打印名称但不打印电子邮件,我收到以下错误:FBSDKLog: 从 Graph API v2.4 开始,对 /me 的 GET 请求应该包含一个明确的“字段”参数,任何人都知道我做错了什么吗?谢谢你

4

3 回答 3

4

我认为您需要添加参数。

尝试将其更改为:

let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
于 2015-09-11T01:40:11.910 回答
1

添加LoginButtonDelegate委托并添加协议,然后添加以下代码并测试

 func loginButton(_ loginButton: FBLoginButton, didCompleteWith result: LoginManagerLoginResult?, error: Error?) {
        print(loginButton)
        let loginManager = LoginManager()
        print(result?.token?.tokenString) //YOUR FB TOKEN
        let req = GraphRequest(graphPath: "me", parameters: ["fields":"email,name"], tokenString: result?.token?.tokenString, version: nil, httpMethod: HTTPMethod(rawValue: "GET"))
        req.start { (connection, resultData, err) in
            if err == nil {
                print(resultData)
            }else {
                print(err)
            }
        }

           loginManager.logIn(permissions: ["email"], from: self) { (result, error) in
             if error != nil {
              // self.showPopup(isSuccess: false)
               return
             }
             guard let token = AccessToken.current else {
               print("Failed to get access token")
           //    self.showPopup(isSuccess: false)
               return
             }

             FirebaseAuthManager().login(credential: FacebookAuthProvider.credential(withAccessToken: token.tokenString)) {[weak self] (success) in
          //     self?.showPopup(isSuccess: true)
             }
           }
    }

    func loginButtonDidLogOut(_ loginButton: FBLoginButton) {
         print(loginButton)
    }
于 2020-01-02T10:02:34.327 回答
1

我也有同样的问题,我找到了目标 c 的解决方案

 NSMutableDictionary*params = [NSMutableDictionary dictionary];
[params setValue:@"fields" forKeyPath:@"fields"];
[params setValue:@"name" forKeyPath:@"name"];
[params setValue:@"email" forKeyPath:@"email"];

并将参数的值设置为 params 这将起作用

于 2015-09-18T08:13:43.540 回答