Ios 使用facebook sdk导入用户已读和想读的书籍

Ios 使用facebook sdk导入用户已读和想读的书籍,ios,facebook-graph-api,Ios,Facebook Graph Api,我正在开发与书籍相关的应用程序。我正在使用me/books获取用户喜欢的书籍,它不需要任何额外的权限,工作正常 现在我希望用户完成并且希望阅读书籍 me/books-无需额外许可 me/books.reads和me/books.want\u-to\u-read是必需的用户操作。books权限 当我尝试以“user\u actions.books”权限登录时。我有以下错误 错误: Error Domain=com.facebook.sdk Code=2“操作无法完成。(com.facebook.s

我正在开发与书籍相关的应用程序。我正在使用me/books获取用户喜欢的书籍,它不需要任何额外的权限,工作正常

现在我希望用户完成并且希望阅读书籍

  • me/books-无需额外许可

  • me/books.readsme/books.want\u-to\u-read是必需的用户操作。books权限

  • 当我尝试以“user\u actions.books”权限登录时。我有以下错误

    错误: Error Domain=com.facebook.sdk Code=2“操作无法完成。(com.facebook.sdk错误2)。”UserInfo=0x10d055b80{com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginCancelled,com.facebook.sdk:ErrorInnerErrorKey=Error Domain=com.apple.accounts Code=7“操作无法完成。(com.apple.accounts error 7.)”,com.facebook.sdk:ErrorSessionKey=,过期日期:(null),刷新日期:(null),尝试刷新日期:0001-12-30 00:00:00+0000,权限:(null)>]

    代码:

    -(void)importBooksFromFacebook{
    
    // If the session state is any of the two "open" states when the button is clicked
    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {
    
        // Close the session and remove the access token from the cache
        // The session state handler (in the app delegate) will be called automatically
        [FBSession.activeSession closeAndClearTokenInformation];
    
        // If the session state is not any of the two "open" states when the button is clicked
    } else {
        // Open a session showing the user the login UI
        // You must ALWAYS ask for public_profile permissions when opening a session
        [FBSession openActiveSessionWithReadPermissions:@[@"user_actions.books"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
    
    
             // If the session was opened successfully
             if (!error && state == FBSessionStateOpen){
                 NSLog(@"Session opened");
                 // Show the user the logged-in UI
                 // successful login now import book using access token
                 return;
             }
             if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
                 // If the session is closed
                 NSLog(@"Session closed");
                 // Show the user the logged-out UI
                 [self userLoggedOut];
             }
    
             // Handle errors
             if (error){
                 NSLog(@"Error");
                 NSString *alertText;
                 NSString *alertTitle;
                 // If the error requires people using an app to make an action outside of the app in order to recover
                 if ([FBErrorUtility shouldNotifyUserForError:error] == YES){
                     alertTitle = @"Something went wrong";
                     alertText = [FBErrorUtility userMessageForError:error];
                     [self showMessage:alertText withTitle:alertTitle];
                 } else {
    
                     // If the user cancelled login, do nothing
                     if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
                         NSLog(@"User cancelled login");
    
                         // Handle session closures that happen outside of the app
                     } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
                         alertTitle = @"Session Error";
                         alertText = @"Your current session is no longer valid. Please log in again.";
                         [self showMessage:alertText withTitle:alertTitle];
    
                         // Here we will handle all other errors with a generic error message.
                         // We recommend you check our Handling Errors guide for more information
                         // https://developers.facebook.com/docs/ios/errors/
                     } else {
                         //Get more error information from the error
                         NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];
    
                         // Show the user an error message
                         alertTitle = @"Something went wrong";
                         alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
                         [self showMessage:alertText withTitle:alertTitle];
                     }
                 }
                 // Clear this token
                 [FBSession.activeSession closeAndClearTokenInformation];
                 // Show the user the logged-out UI
                 //[self userLoggedOut];
             }
         }];
    }
    
    }
    

    我通过以下回答解决了这个问题:

    if ([[FBSession activeSession] isOpen]) {
      /* 
       * if the current session has no publish permission we need to reauthorize 
       */
      if ([[[FBSession activeSession] permissions]indexOfObject:@"publish_actions"] == NSNotFound) {
    
            [[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"] defaultAudience:FBSessionDefaultAudienceFriends
                                                  completionHandler:^(FBSession *session,NSError *error){
                                                      [self postPhoto];
                                                  }];
    
        }else{
            [self publishStory];
        }
    }else{
        /* 
         * open a new session with publish permission 
         */
        [FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                           defaultAudience:FBSessionDefaultAudienceOnlyMe
                                              allowLoginUI:YES
                                         completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                             if (!error && status == FBSessionStateOpen) {
                                                 [self publishStory];
                                             }else{
                                                 NSLog(@"error");
                                             }
                                         }];
    }