S. Karthik S. Karthik - 5 months ago 101
Objective-C Question

Error: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" = DiBO16GER0C;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}, 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:FBSDKGraphRequestErrorCategoryKey=0}


I am facing the above error. I know this was already discussed but they fixed using
FBSession
. In the Latest SDK i did not see the FBSession. Please help me i am stuck on this for whole day.

Issue coming under:
I have 3 pages (Login page, username page, find friends page). In login page i just authenticate with facebook and get the user information from the graphAPI it works fine. Then come to the page username here normal registration process from user. Then i come to the find friends page here i facing the issue that is, If i come with the flow(login -> username -> find friends page) i can list the facebook friends using graphAPI BUT i could not get the friends list when i kill the app and open this page directly it shows above error. I dont know how to get back from this. Please help anybody felt this before.

My code are below,
Find Friends Page:

if ([FBSDKAccessToken currentAccessToken])
{
[[[FBSDKGraphRequest alloc] initWithGraphPath:url parameters:@{ @"fields": @"id,picture"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
{
NSLog(@"result === %@",result);
if (!error)
{}
else
{}
}];
}
else
{
NSLog(@"There is no current access token");
}


Note:
The current access token is null when i kill the app and come back to the username page again.

Answer

Got it!

This will be surely helpful to someone, Actually i did mistake in this place, [[[FBSDKGraphRequest alloc] initWithGraphPath:url parameters:@{ @"fields": @"id,picture"}] This method not have the access token ever. But wonder is the access token is not enable in the whole app. it will be null when i move to the next page after kill the app. So i just save the token string in NSUserDefaults like

NSString *access_token=[FBSDKAccessToken currentAccessToken].tokenString;
[NSUSER setObject:access_token forKey:@"fb_token"];
 [NSUSER synchronize];

And then wherever i call the graphAPI i use this method with tokenstring like,

[[[FBSDKGraphRequest alloc] initWithGraphPath:url parameters:@{ @"fields": @"id,picture"} tokenString:[NSUSER objectForKey:@"fb_token"] version:nil HTTPMethod:@"GET"]
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
             {
}];

Simple but it takes too long to find. Glad!