iOS Facebook SDK: An active access token must be used to query information about the current user
Asked Answered
O

5

13

I have followed many examples and looked at many questions regarding this access token error, but I can't seem to find out why I am getting it or how to fix it.

I init Facebook like they do in the 3.1 sdk examples:

 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];
     }];

This works fine and the user is asked to accept the app permissions in Safari and is returned to the app afterwards. When the user is returned to the app, my FBSessionState is FBSessionStateOpen.

When I try this:

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

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

or this:

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];
                                        }];

but I get:

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}}

I have taken the access token from my FBSession and put it into Facebook's Access Token Debugger and it shows I have a good token:

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

I must be missing some small detail in the settings or something.

Thank you all in advance!

Oballa answered 29/10, 2012 at 18:8 Comment(0)
L
29

According to the docs, https://developers.facebook.com/docs/reference/ios/3.1/class/FBRequestConnection

"The request uses the active session represented by [FBSession activeSession]."

This applies to the static methods dealing with requests. So what you want to do is make sure you set the active session. Do the following:

[FBSession setActiveSession:self.fb];

Just before you call the first FBRequestConnection start* method.

Laevorotatory answered 31/10, 2012 at 21:47 Comment(3)
Thank you! I knew it had to be something small :)Oballa
@C Abernathy What is "FBSession" ? in built class ? i cant import this in my project ? any additional framwork required to do this ?Phocomelia
FBSession is a class in the Facebook SDK for iOS. For more details integrating with the SDK, start here: developers.facebook.com/iosLaevorotatory
N
11

Well After some time, noticing that FBSession.activeSession was not nil, what I was missing before sending the request was:

    if (!FBSession.activeSession.isOpen) {
        [FBSession openActiveSessionWithAllowLoginUI: YES];
    }
Namtar answered 7/4, 2013 at 21:11 Comment(1)
I didn't really find the accepted answer resolved my issues but this one did.Capitulation
S
1

I got this error in v4 of the SDK when attempting to use a FBSDKGraphRequest object that had been created before logging in. When I changed this to create the FBSDKGraphRequest immediately before using it (i.e. after having logged in) it worked fine. It seems the initializer for this class sets some token that isn't refreshed when the request is started.

I tried updating the request's tokenString property after logging in, but for some reason this is a readonly property.

Sociometry answered 1/4, 2015 at 21:34 Comment(0)
H
0

/* deprecated for google and facebook func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: AnyObject) -> Bool { let googleDidHandle = GIDSignIn.sharedInstance().handle(url, sourceApplication: sourceApplication, annotation: annotation)

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

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

So i used, 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 beta 3 , ios 10

Hutch answered 11/8, 2016 at 12:53 Comment(0)
H
0

you can try this too, works for me

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"])
}
Hutch answered 11/8, 2016 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.