如何从 facebook sdk 4 获取用户电子邮件 swift
How to get user email from facebook sdk 4 whith swift
我一直在尝试使用此代码来获取我的 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 请求应该包含一个明确的 "fields" 参数,谁知道我做错了什么?谢谢
我认为您需要添加参数。
尝试将其更改为:
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
我也有同样的问题,我找到了 objective c
的解决方案
NSMutableDictionary*params = [NSMutableDictionary dictionary];
[params setValue:@"fields" forKeyPath:@"fields"];
[params setValue:@"name" forKeyPath:@"name"];
[params setValue:@"email" forKeyPath:@"email"];
并将参数的值设置为 params 这样就可以了
添加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)
}
我一直在尝试使用此代码来获取我的 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 请求应该包含一个明确的 "fields" 参数,谁知道我做错了什么?谢谢
我认为您需要添加参数。
尝试将其更改为:
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields":"name, email"])
我也有同样的问题,我找到了 objective c
的解决方案 NSMutableDictionary*params = [NSMutableDictionary dictionary];
[params setValue:@"fields" forKeyPath:@"fields"];
[params setValue:@"name" forKeyPath:@"name"];
[params setValue:@"email" forKeyPath:@"email"];
并将参数的值设置为 params 这样就可以了
添加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)
}