我正在使用图表api从Facebook登录名返回我的姓名和电子邮件。这是我使用的代码 - 名称返回正常,但电子邮件是零:
func getUserInfo() {
let params = ["fields" : "email, name"]
let graphRequest = GraphRequest(graphPath: "me", parameters: params)
graphRequest.start {
(urlResponse, requestResult) in
switch requestResult {
case .failed(let error):
print("error in graph request:", error)
break
case .success(let graphResponse):
if let responseDictionary = graphResponse.dictionaryValue {
print(responseDictionary)
if let name = responseDictionary["name"] {
self.nameLabel.text = "Logged in as: \n\(name)"
}
if let email = responseDictionary["email"] {
self.emailLabel.text = "Your email is: \n\(email)"
} else {
self.emailLabel.text = "Your email is: "
}
print(responseDictionary["name"])
print(responseDictionary["email"])
}
}
}
}
这是打印到我的控制台的内容:
["id": 1653899307960705, "name": *my name removed for privacy*]
Optional(*my name removed for privacy*)
nil
我不确定我的代码是错误还是只是一个错误 - 我将电子邮件和名称都指定为我的参数,但正如您所看到的,只有名称会回来。任何人都知道为什么会这样吗?
答案 0 :(得分:1)
您的代码绝对正确,Facebook图形API无法提供电子邮件的可能性很小:
检查/您的帐户的帐户设置(电子邮件有效且已经过验证,并且是公开的)
答案 1 :(得分:1)
我忘了接受电子邮件许可,所以我在这里分享您的代码以获得许可。
func fbLoginInitiate()
{
let loginManager = FBSDKLoginManager()
loginManager.logInWithReadPermissions(["public_profile", "email","user_friends"], handler: {(result:FBSDKLoginManagerLoginResult!, error:NSError!) -> Void in
if (error != nil) {
// Process error
self.removeFbData()
} else if result.isCancelled {
// User Cancellation
self.removeFbData()
} else {
//Success
if result.grantedPermissions.contains("email") && result.grantedPermissions.contains("public_profile") && result.grantedPermissions.contains("user_friends"){
//Here you need to call your function for graphRequest
self.getUserInfo()
} else {
//Handle error
}
}
})
}
func removeFbData()
{
//Remove FB Data
let fbManager = FBSDKLoginManager()
fbManager.logOut()
FBSDKAccessToken.setCurrentAccessToken(nil)
}