简体   繁体   中英

iOS: Get Facebook Access Token with Auto-Login

I'm using Parse and Facebook for account management in my app. Whenever the user logs in through the facebook button, my code works perfectly because facebook sdk automatically generates a new access token. However, if I use an autologin code which checks whether user has already approved of the app, I have no access token and can't access facebook data for that user. I don't know how to request an access token for a user who has already agreed to use my app.

Loading data:

- (void)_loadData :(BOOL)updateData
{
    NSLog(@"entered _loadData");
    NSMutableDictionary* userInfoParams = [NSMutableDictionary dictionary];
    [userInfoParams setValue:@"id,name,email,gender" forKey:@"fields"];

    if([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Expiration date of token: %@", [[FBSDKAccessToken currentAccessToken] expirationDate]);
    }
    else NSLog(@"No access token");


    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:userInfoParams];
    [request startWithCompletionHandler:^(FBSDKGraphRequestConnection* connection, id result, NSError* error)
     {
         if(!error)
         {
             ...
         }
      }
}

Auto-login system:

 (void)viewDidAppear:(BOOL)animated
{
    if ([PFUser currentUser] || [PFFacebookUtils isLinkedWithUser:[PFUser currentUser]])
    {
        NSLog(@"Yes");
        [self _loadData:YES];
        [self transitionToLoginSegue:(id)self];
    }
    else NSLog(@"No");
}

Also, the access tokens I receive when user clicks through the log in button last for 4 weeks, so if there was a way to manually assign an access token then I could do that, however [FBSDKAccessToken currentAccessToken] is not assignable so I can't do that either.

This is my code below,

 if ([self check_network])
      {

      START_LOAD;
      FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
      [login
       logInWithReadPermissions:  @[@"public_profile",@"email",@"user_friends"]
       fromViewController:self
       handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
             if (error) {
                   STOP_LOAD;
                   NSLog(@"Process error");
                   NSLog(@"%@",error);
                   TOAST_FOR_TRY_AGAIN;
                   /*
                   UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Please Try Again"
                                                                 message:nil
                                                                delegate:self
                                                       cancelButtonTitle:@"Ok"
                                                       otherButtonTitles: nil];
                   [alert show];
                    */
             } else if (result.isCancelled)
             {
                    STOP_LOAD;
                   NSLog(@"Cancelled");
             } else
             {
                   NSLog(@"Logged in");
                   NSLog(@"Result=%@",result);

                   [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{ @"fields": @"id,first_name,middle_name,last_name,name,picture,email"}]
                    startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
                    {
                          NSLog(@"Facebook result=%@",result);
                         if (!error)
                             {
                                [NSUSER setObject:result forKey:@"user_info"];
                                [NSUSER setObject:[result objectForKey:@"name"] forKey:@"skip_name"];
                                [NSUSER synchronize];

                                API_CALL_ALLOC(ls_apicall);
                                [ls_apicall Login: STRING([result objectForKey:@"id"])];

                          } else {
                                 STOP_LOAD;
                                NSLog(@"An error occurred getting friends: %@", [error localizedDescription]);
                          }
                    }];
             }
       }];
      }
      else
      {
            TOAST(TOAST_NETWORK_ERROR);
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM