0

iOS 앱에서 Facebook API를 사용하여 Facebook과 공유를 요청했지만 이제 Facebook에서이 상태 코드를 수신합니다. 그래서 내 질문 방식이있다,Social API Facebook iOS : "액세스 토큰의 유효성 검사 오류 : 세션이 만료되었습니다."

ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 

NSDictionary *options = @{ACFacebookAppIdKey: @"##myfbid###",ACFacebookPermissionsKey: @[@"email"]}; 

[accountStore requestAccessToAccountsWithType:facebookTypeAccount 
    options:options 
    completion:^(BOOL granted, NSError *error) { 
     if(granted){ 
      NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount]; 
      facebookAccount = [accounts lastObject]; 
      //NSLog(@"Success"); 

      NSURL *meurl = [NSURL URLWithString:@"https://graph.facebook.com/me"]; 
      SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook 
       requestMethod:SLRequestMethodGET 
       URL:meurl 
       parameters:nil]; 
      merequest.account = facebookAccount; 
      [merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
       NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error]; 
       //this json conaints the log i write above 
     } 

]; 

그래서 내가 다시 로그인 한 후 아이폰 설정에서 페이스 북 계정에서 로그 아웃하고 시도 :

{ 
    error =     { 
        code = 190; 
        "error_subcode" = 463; 
        message = "Error validating access token: Session has expired on 25 marzo 2014 16.20. The current time is 18 aprile 2014 8.45."; 
        type = OAuthException; 
    }; 
} 

내가 사용하는 코드입니다 이 문제를 피하기 위해 앱이 열릴 때마다 자동으로이 토큰을 새로 고칩니다.

답변

4

"자동으로"액세스 토큰을 새로 고칠 수 없습니다. 응답을받을 때마다 오류를 확인하고 오류가 있으면 토큰을 갱신해야합니다. 이 코드는 도움이 될 수 있습니다.

.... 
if([json.profileDictionary objectForKey:@"error"]!=nil){ 
    [self renewCredentials]; 
} 

-(void)renewCredentials{ 
    [self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){ 
     if(!error){ 
      switch (renewResult) { 
       case ACAccountCredentialRenewResultRenewed: 
        NSLog(@"Good to go"); 
        [self getFacebookAccount]; 
        break; 
       case ACAccountCredentialRenewResultRejected: 
        NSLog(@"User declined permission"); 
        break; 
       case ACAccountCredentialRenewResultFailed: 
        NSLog(@"non-user-initiated cancel, you may attempt to retry"); 
        break; 
       default: 
        break; 
      } 
     } 
     else{ 
      //handle error 
      NSLog(@"error from renew credentials%@",error); 
     } 
    }]; 
} 

희망이 있습니다.