导入用户阅读并想要使用facebook sdk阅读书籍

时间:2014-08-18 10:23:49

标签: ios facebook-graph-api

我正在处理与图书相关的应用。我正在使用我/书籍获取用户喜欢的书籍,并且不需要任何额外的权限和工作正常。

现在我希望用户已完成想要阅读图书

  1. me / books - 无需额外许可

  2. me / books.reads me / books.wants_to_read 是必需的 user_actions.books 权限。

    < / LI>

    当我尝试使用“user_actions.books”权限登录时。我收到了以下错误。

    错误: Error Domain = com.facebook.sdk Code = 2“无法完成操作。(com.facebook.sdk error 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 =, expirationDate:(null),refreshDate:(null),attemptsRefreshDate:0001-12-30 00:00:00 +0000,permissions:(null)&gt;}

    代码:

    -(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];
             }
         }];
    }
    
    }
    

1 个答案:

答案 0 :(得分:0)

我通过下面的stackoverflow回答解决了这个问题:

https://stackoverflow.com/a/14696106/1237937

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");
                                         }
                                     }];
}