sanandiya vipul sanandiya vipul - 5 months ago 38
iOS Question

Facebook Error Invalid_access token in ios 9.2



  1. In ios 9.2 when i login facebook get error Invalid_access token.

  2. But when i login with app register account it get successfully login facebook.

  3. my image here.




enter image description here

MYviewController.m file


-(void)loginButtonClicked
{
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
login.loginBehavior = FBSDKLoginBehaviorWeb;
[login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
}
else {

if ([result.grantedPermissions containsObject:@"email"]) {

if ([FBSDKAccessToken currentAccessToken]) {
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, picture.type(normal), accounts{username},email, gender, locale, timezone, about"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"fetched user:%@", result);

NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;
NSLog(@"fbAccessToken=>%@", fbAccessToken);

}
}];
}
}
}
}];
}





MYAppdelegate.m file.


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {

[FBSDKAppEvents activateApp];
}




Answer

try this

-(void)loginButtonClicked
{

  /*********  logout the current session ************/
   FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
  [login logOut];
  [FBSDKAccessToken setCurrentAccessToken:nil];
  [FBSDKProfile setCurrentProfile:nil];
/*********  logout the current session ************/

   /*********  start the new session for login ************/

  // FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    login.loginBehavior = FBSDKLoginBehaviorWeb;
   [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error
        } else if (result.isCancelled) {
            // Handle cancellations
        }
        else {

            if ([result.grantedPermissions containsObject:@"email"]) {

                if ([FBSDKAccessToken currentAccessToken]) {
                   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"first_name, last_name, picture.type(normal), accounts{username},email, gender, locale, timezone, about"}]
                     startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                         if (!error) {
                             NSLog(@"fetched user:%@", result);

                            NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;
                            NSLog(@"fbAccessToken=>%@", fbAccessToken);

                         }
                     }];
                }
            }
        }
    }];
}