gpt4 book ai didi

使用 Google Firebase 的 iOS Swift Facebook 身份验证 - 如何获取额外权限?

转载 作者:搜寻专家 更新时间:2023-11-01 06:37:55 25 4
gpt4 key购买 nike

我使用最新版本的 Facebook SDK 4.15 和 Google Firebase 来验证用户。我正在关注此处的文档:https://firebase.google.com/docs/auth/ios/facebook-login

这对我有用。

但是我想从 Facebook 用户那里获得额外的权限,比如电子邮件。我已经像这样修改了代码:

@IBOutlet var facebookButton : FBSDKLoginButton!


override func viewDidLoad() {
/...
facebookButton.readPermissions = ["email","public_profile"]
...
}

有了这个,当我通过 Facebook 授权我的应用程序时,我得到了 Facebook 确认对话框,要求电子邮件许可。这样就可以了。

但是在我的回调中,用户的电子邮件地址始终为零:

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
if let error = error {
print(error.localizedDescription)
return
}

let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential) { (user, error) in
// ...
if let error = error {
print(error.localizedDescription)
return
}

print("user.uid \(user!.uid)")
print("user.photoURL \(user!.photoURL)")
print("user.providerID \(user!.providerID)")
print("user.email \(user!.email)")
print("user.displayName \(user!.displayName)")

如何在使用 Google Firebase 身份验证时通过 Facebook 登录获取电子邮件等额外权限?

最佳答案

使用第三方身份验证时,您会取回身份验证 token ,然后使用该 token 让用户登录您的应用。有了这个 token ,Firebase Auth 就可以提取用户的电子邮件地址。但是对于您的情况,如果您希望在 FB 登录后获取用户的电子邮件地址,则需要使用 Facebook 的 Graph API。您只需创建一个包含您要求的信息的 GraphRequest,如下所示。

swift 2

struct FacebookPermission
{
static let ID: String = "id"
static let NAME: String = "name"
static let EMAIL: String = "email"
static let PROFILE_PIC: String = "picture"
static let LAST_NAME: String = "last_name"
static let FIRST_NAME: String = "first_name"
static let USER_FRIENDS: String = "user_friends"
static let PUBLIC_PROFILE: String = "public_profile"
}

func getFacebookData()
{
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "\(FacebookPermission.NAME), \(FacebookPermission.FIRST_NAME), \(FacebookPermission.LAST_NAME), \(FacebookPermission.EMAIL), \(FacebookPermission.PROFILE_PIC).type(large)"])

graphRequest.startWithCompletionHandler { (connection: FBSDKGraphRequestConnection!, result:AnyObject!, error: NSError!) in

if error == nil
{
if let userName = result.valueForKey(FacebookPermission.NAME) as? String
{
self.currentUser.userName = userName
}

if let firstName = result.valueForKey(FacebookPermission.FIRST_NAME) as? String
{
self.currentUser.firstName = firstName
}

if let lastName = result.valueForKey(FacebookPermission.LAST_NAME) as? String
{
self.currentUser.lastName = lastName
}

if let email = result.valueForKey(FacebookPermission.EMAIL) as? String
{
self.currentUser.email = email
}

if let profilePic = result.valueForKey(FacebookPermission.PROFILE_PIC)
{
let facebookImageURL = profilePic.valueForKey("data")?.valueForKey("url") as? String

if let unwrappedURL = facebookImageURL
{
let imageData: NSData = NSData(contentsOfURL: NSURL(string: unwrappedURL)!)!
self.currentUser.profileImage = UIImage(data: imageData)!
}
}
}
else
{
print("Facebook Graph Request Error")
}
}
}

swift 3

func getFacebookData()
{
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields" : "\(FacebookPermission.NAME), \(FacebookPermission.FIRST_NAME), \(FacebookPermission.LAST_NAME), \(FacebookPermission.EMAIL), \(FacebookPermission.PROFILE_PIC).type(large)"])

graphRequest.start { (connection: FBSDKGraphRequestConnection?, result: Any?, error: Error?) in

if error == nil
{
if let facebookData = result as? NSDictionary
{
if let userName = facebookData.value(forKey: FacebookPermission.NAME) as? String
{
self.currentUser.userName = userName
}

if let firstName = facebookData.value(forKey: FacebookPermission.FIRST_NAME) as? String
{
self.currentUser.firstName = firstName
}

if let lastName = facebookData.value(forKey: FacebookPermission.LAST_NAME) as? String
{
self.currentUser.lastName = lastName
}

if let email = facebookData.value(forKey: FacebookPermission.EMAIL) as? String
{
self.currentUser.email = email
}

if let profilePic = facebookData.value(forKey: FacebookPermission.PROFILE_PIC)
{
let facebookImageURL = ((profilePic as AnyObject).value(forKey: "data") as AnyObject).value(forKey: "url") as? String

if let unwrappedURL = facebookImageURL
{
let imageData: Data = try! Data(contentsOf: URL(string: unwrappedURL)!)
self.currentUser.profileImage = UIImage(data: imageData)!
}
}
}
}
else
{
print("Facebook Graph Request Error")
}
}
}

获取他们的信息后,您可以继续将其存储到您的 Firebase 数据库中。

Here's有关 Facebook 权限的更多信息。伙计,干杯!

关于使用 Google Firebase 的 iOS Swift Facebook 身份验证 - 如何获取额外权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39297568/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com