我正在尝试将Facebook登录与swift 3.0集成到iOS 10应用程序中。在将FacebookSDK安装到应用程序后,我得到如下所示的错误代码2500:
控制台错误码:body = {
error = {
code = 2500;
"fbtrace_id" = FpAkfsaBAFc;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}})
loginViewController上的loginButton函数:
func loginButton(_ loginButton: FBSDKLoginButton!, didCompleteWith result: FBSDKLoginManagerLoginResult!, error: Error!) {
fetchProfile()
print("@@@@Completed Login@@@@")
}
在loginVC上获取Facebook Profile函数:
func fetchProfile() {
let parameters = ["fields": "email, first_name, last_name, picture.type(large)"]
FBSDKGraphRequest(graphPath: "me", parameters: parameters).start(completionHandler: { (connection, user, requestError) -> Void in
if requestError != nil {
print(requestError)
print("There are some error during getting the request from fb")
return
}
self.email = (user?["email"] as? String)!
self.firstName = (user?["first_name"] as? String)!
self.lastName = (user?["last_name"] as? String)!
self.nameLabel.text = "(self.firstName) (self.lastName)"
print("My fbname is " + self.firstName)
print(self.lastName)
var pictureUrl = ""
if let picture = user?["picture"] as? NSDictionary, let data = picture["data"] as? NSDictionary, let url = data["url"] as? String {
pictureUrl = url
print(pictureUrl)
}
})
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}
在堆栈溢出上搜索过类似的问题,但没有在swift 3.0上工作,如果有人能就这个问题提出建议,我会很感激,谢谢,祝你有一个美好的一天!
如错误消息所述,您缺少access_token。在发出请求之前创建一个控件,并检查access_token是否为nil,然后请求一个新的。之后,您应该能够提出您的请求。
我已经解决了这个问题,因为在iOS10中默认禁用了keychain访问,在Capabilities下启用 keychain Sharing和Facebook登录是完美的!