xcode Swift 获取 Facebook AccessToken
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42802404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift get Facebook AccessToken
提问by user2423476
I'm trying to obtain the Facebook Accesstoken. when I print accessToken it returns a object with appId and authenticationToken, How do I parse out the token? I am using the following code:
我正在尝试获取 Facebook Accesstoken。当我打印 accessToken 时,它返回一个带有 appId 和 authenticationToken 的对象,我该如何解析令牌?我正在使用以下代码:
let loginManager = LoginManager()
loginManager.logIn([ .publicProfile, .userFriends, .email ], viewController: self) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("accessToken: " + String(describing: accessToken))
break
}
}
回答by Rafael Flecha
The token you're trying to parse is a struct
您尝试解析的令牌是一个结构体
the actualtoken is:
在实际的令牌是:
accessToken.authenticationToken
your code should be like that:
你的代码应该是这样的:
let loginManager = LoginManager()
loginManager.logIn([ .publicProfile, .userFriends, .email ], viewController: self) { loginResult in
switch loginResult {
case .failed(let error):
print(error)
case .cancelled:
print("User cancelled login.")
case .success(let grantedPermissions, let declinedPermissions, let accessToken):
print("accessToken: " + accessToken.authenticationToken)
break
}
}