[iOS] Facebook エラー:The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time.

Tadashi Shigeoka ·  Wed, May 22, 2013

Facebook iOS SDK version 3.5.1 で Facebook 認証を実装したらエラーが発生しました。

エラーログ

error:Error Domain=com.facebook.sdk Code=2 “The operation couldn’t be completed. (com.facebook.sdk error 2.)” UserInfo=0x1e8b2740 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:SystemLoginCancelled, com.facebook.sdk:ErrorInnerErrorKey=Error Domain=com.apple.accounts Code=7 “The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time.” UserInfo=0x1ddf1eb0 {NSLocalizedDescription= The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time. }, com.facebook.sdk:ErrorSessionKey=<FBSession: 0x1ddabd90, state: FBSessionStateClosedLoginFailed, loginHandler: 0x0, appID: 336861069774973, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x1dd91670>, expirationDate: (null), refreshDate: (null), attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(null)>}

最初、以下のように openActiveSessionWithPublishPermissions で、publish_actions だけ取得しようとしていたのですが「The app must ask for a basic read permission at install time.」というエラーメッセージの通り、まず basic profile information を取得する必要があるそうです。

+ (void)connectWithCompletionHandler:(void(^)(FBSession *session, FBSessionState status, NSError *error))handler {
    NSLog(@"Connection");
    if ([[FBSession activeSession] isOpen]) {
        NSLog(@"Logout");
        [[FBSession activeSession] closeAndClearTokenInformation];
    }

    // Request publish permissions
    NSArray *publishPermissions = [NSArray arrayWithObjects:@"publish_actions", nil];
    [FBSession openActiveSessionWithPublishPermissions:publishPermissions
                                       defaultAudience:FBSessionDefaultAudienceFriends
                                          allowLoginUI:YES
                                     completionHandler:handler];
}

なので、下記のように先に openActiveSessionWithReadPermissions を呼び出せばOKです。

+ (void)connectWithCompletionHandler:(void(^)(FBSession *session, FBSessionState status, NSError *error))handler {
    NSLog(@"Connection");
    if ([[FBSession activeSession] isOpen]) {
        NSLog(@"Logout");
        [[FBSession activeSession] closeAndClearTokenInformation];
    }

    // Request basic profile information
    // 基本情報で必要なものがある場合、指定する
    // NSArray *readPermissions = [NSArray arrayWithObjects:@"email", @"user_birthday", nil];
    NSArray *readPermissions = nil;
    [FBSession openActiveSessionWithReadPermissions:readPermissions
                                       allowLoginUI:NO
                                  completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
                                      /* handle success + failure in block */
                                  }];

    // Request publish permissions
    NSArray *publishPermissions = [NSArray arrayWithObjects:@"publish_actions", nil];
    [FBSession openActiveSessionWithPublishPermissions:publishPermissions
                                       defaultAudience:FBSessionDefaultAudienceFriends
                                          allowLoginUI:YES
                                     completionHandler:handler];
}

[参考]

Facebook iOS SDK3.5で投稿処理を行う際の流れ(その2) - assaulter’s diary