13

我遵循了许多示例并查看了有关此访问令牌错误的许多问题,但我似乎无法找出我得到它的原因或如何修复它。

我像在 3.1 sdk 示例中那样初始化 Facebook:

 NSArray* permissions = [[NSArray alloc] initWithObjects:@"publish_stream", @"publish_actions", nil];
 self.fb = [[FBSession alloc] initWithPermissions:permissions];
 [self.fb openWithCompletionHandler:^(FBSession *session,
                                         FBSessionState status,
                                         NSError *error)
     {
         [self sessionStateChanged:session :status :error];
     }];

这工作正常,用户被要求接受 Safari 中的应用程序权限,然后返回到应用程序。当用户返回到应用程序时,我的 FBSessionState 是 FBSessionStateOpen。

当我尝试这个时:

[FBRequestConnection startForMeWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
                 {

                     [self showAlert:@"logged in?" result:result error:error];
                 }];

或这个:

NSMutableDictionary* fbPost = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"post from iOS", @"message", nil];
[FBRequestConnection startWithGraphPath:@"me/feed" parameters:fbPost HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
                                        {

                                            [self showAlert:@"message posted?" result:result error:error];
                                        }];

但我得到:

FBSDKLog: FBURLConnection <#1126>:

  Duration: 936 msec

Response Size: 0 kB

  MIME type: text/javascript

  Response:

{"error":{"message":"An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}

我从我的 FBSession 中获取了访问令牌并将其放入 Facebook 的访问令牌调试器中,它表明我有一个好的令牌:

App ID: <hidden>
Metadata: {"sso":"iphone-safari"}
User ID: <hidden>
Issued:  1351525817 (about an hour ago)
Expires: 1356709817 (in about 2 months)
Valid:  True
Origin: Mobile Web Faceweb
Scopes: create_note photo_upload publish_actions publish_stream share_item status_update video_upload

我必须在设置或其他东西中遗漏一些小细节。

谢谢大家!

4

5 回答 5

29

根据文档,https://developers.facebook.com/docs/reference/ios/3.1/class/FBRequestConnection

“该请求使用由 [FBSession activeSession] 表示的活动会话。”

这适用于处理请求的静态方法。因此,您要做的是确保设置活动会话。请执行下列操作:

[FBSession setActiveSession:self.fb];

就在您调用第一个 FBRequestConnection start* 方法之前。

于 2012-10-31T21:47:56.777 回答
11

好吧,过了一段时间,注意到 FBSession.activeSession 不是零,我在发送请求之前缺少的是:

    if (!FBSession.activeSession.isOpen) {
        [FBSession openActiveSessionWithAllowLoginUI: YES];
    }
于 2013-04-07T21:11:29.217 回答
1

尝试使用FBSDKGraphRequest登录前创建的对象时,我在 SDK 的 v4 中遇到此错误。当我将其更改为FBSDKGraphRequest在使用它之前立即创建(即登录后)时,它工作正常。似乎此类的初始化程序设置了一些在请求启动时未刷新的令牌。

登录后我尝试更新请求的tokenString属性,但由于某种原因,这是一个readonly属性。

于 2015-04-01T21:34:08.947 回答
0

/* 不推荐用于 google 和 facebook func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool { let googleDidHandle = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication,注释:注释)

    let facebookDidHandle = FBSDKApplicationDelegate.sharedInstance().application(
        application,

open: url, sourceApplication: sourceApplication, annotation: annotation) print(facebookDidHandle) return googleDidHandle || facebookDidHandle } */

所以我使用了 func application(_ application: UIApplication, open url: URL, options: [String: AnyObject]) -> Bool {

    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: options) || GIDSignIn.sharedInstance().handle(url as URL!, sourceApplication: options[UIApplicationOpenURLOptionsSourceApplicationKey] as! String, annotation: options[UIApplicationOpenURLOptionsAnnotationKey])
}

xcode 8 测试版 3,iOS 10

于 2016-08-11T12:53:13.817 回答
0

你也可以试试这个,对我有用

func application(_ application: UIApplication,
                      open url: URL, 
                       options: [String: AnyObject]) -> Bool
{
    let delegate = FBSDKApplicationDelegate.sharedInstance()

    return delegate.application(application, open: url, 
                                sourceApplication: options["UIApplicationOpenURLOptionsSourceApplicationKey"] as! String, 
                                       annotation: options["UIApplicationOpenURLOptionsAnnotationKey"])
}
于 2016-08-11T14:23:58.383 回答