适用于 iOS 访问令牌问题的 Facebook SDK
Posted
技术标签:
【中文标题】适用于 iOS 访问令牌问题的 Facebook SDK【英文标题】:Facebook SDK for iOS Access Token Issue 【发布时间】:2015-11-28 02:27:08 【问题描述】:我正在尝试在我的 ios swift 应用程序中使用 Facebook 来获取一些基本的用户信息。我使用 Cocoapods 安装 Facebook 依赖项。我已使用以下链接更新了我的 info.plist。
https://developers.facebook.com/docs/ios/ios9 https://developers.facebook.com/quickstarts/115956755431487/?platform=ios
每当我尝试通过 facebook 登录时,都会收到以下编译器错误:
-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
-canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
Error: Error Domain=com.facebook.sdk.core Code=8 "(null)" UserInfo=com.facebook.sdk:FBSDKGraphRequestErrorCategoryKey=0, com.facebook.sdk:FBSDKGraphRequestErrorHTTPStatusCodeKey=400, com.facebook.sdk:FBSDKErrorDeveloperMessageKey=An active access token must be used to query information about the current user., com.facebook.sdk:FBSDKGraphRequestErrorGraphErrorCode=2500, com.facebook.sdk:FBSDKGraphRequestErrorParsedJSONResponseKey=
body =
error =
code = 2500;
"fbtrace_id" = F1CmQQhd4pA;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
;
;
code = 400;
播客文件
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
pod 'FBSDKCoreKit', '~> 4.8'
pod 'FBSDKLoginKit', '~> 4.8'
pod 'FBSDKShareKit', '~> 4.8'
带有 FBSDKLoginButtonDelegate 的 ViewController
override func viewDidLoad()
super.viewDidLoad()
var loginButton = FBSDKLoginButton()
loginButton.readPermissions = ["user_about_me", "email", "public_profile"]
loginButton.center = self.view.center
loginButton.delegate = self
self.view.addSubview(loginButton)
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!)
let fields = "email, name, first_name, last_name"
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphpath: "me", parameters: ["fields" : fields])
graphRequest.startWithCompletionHandler( (connection, result, error) -> Void in
if ((error) != nil)
print("Error: \(error)")
)
应用代理
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
func applicationDidBecomeActive(application: UIApplication)
FBSDKAppEvents.activateApp()
其他信息:Swift 2、Xcode 7、iOS 9
任何解决此问题的建议将不胜感激。
【问题讨论】:
【参考方案1】:我的错误是我的 AppDelegate 中有以下两个函数。不推荐使用带有 sourceApplication 的 openURL 方法,因此应用程序将使用更新的方法。但是,Facebook 自己的文档声明使用已弃用的版本,这导致我的错误位于此处:https://developers.facebook.com/docs/ios/getting-started
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool
Facebook 声明使用已弃用的 openURL http://imgur.com/kwI5VId
【讨论】:
我删除了较新的委托回调:application(app: UIApplication, openURL url: NSURL, options ...) 以解决当时的问题。 哇,这对我也有用。我越读 facebook 的开发者文档,我就越生气……以上是关于适用于 iOS 访问令牌问题的 Facebook SDK的主要内容,如果未能解决你的问题,请参考以下文章
facebook javascript api 在帖子上出现错误,例如“(#3)通过 API 发布喜欢仅适用于页面访问令牌”
使用 Facebook 的访问令牌在 UIWebView 中显示带有经过身份验证的用户会话的粉丝页面