openActiveSessionWithPublishPermissions / requestNewPublishPermissions publish_actions未添加权限

时间:2015-01-25 02:41:27

标签: ios facebook facebook-ios-sdk facebook-permissions

使用SDK 3.22.0。

if (FBSession.activeSession.isOpen) {
    [FBSession.activeSession
     requestNewPublishPermissions:@[@"publish_actions"]
     defaultAudience:FBSessionDefaultAudienceFriends
     completionHandler:^(FBSession *session, NSError *error) {
         NSLog(@"error = %@", error);
         NSLog(@"session open = %d", session.isOpen);
         NSLog(@"session.permissions = %@", session.permissions);
         NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
     }];
}
else {
    [FBSession
     openActiveSessionWithPublishPermissions:@[@"publish_actions"]
     defaultAudience:FBSessionDefaultAudienceFriends
     allowLoginUI:YES
     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
         NSLog(@"error = %@", error);
         NSLog(@"session open = %d", session.isOpen);
         NSLog(@"status = %lu", status);
         NSLog(@"session.permissions = %@", session.permissions);
         NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);
     }];
}

我正在测试没有发布权限的用户,也从未拒绝过。在此帐户的FB应用设置上,应用可见性设置为“好友”,发布权限甚至不在设置列表中,因为它们适用于其他用户/应用。

在这两种情况下,FB应用程序都会打开并立即返回我的应用程序,而不会询问权限。 declinedPermissions的回复是publish_actions的数组。

我的期望是FB应用程序会要求用户批准发布。

我收到了多个用户遇到同样问题的报告 - 无法添加发布权限,但有些用户可以获得权限。

要添加的一件事是,在向测试组之外的用户提交FB批准的应用程序之前,我遇到了同样的问题,但是当应用程序获得批准后,它开始为这些用户工作。现在看来,即使应用程序获得批准,问题仍然存在,仅供随机用户使用。

我对请求权限的方式有什么不妥吗?

2 个答案:

答案 0 :(得分:2)

看起来这样有效:

- (BOOL)hasWritePermissions {
    if (!FBSession.activeSession.isOpen) return NO;
    return [FBSession.activeSession.permissions indexOfObject:@[@"publish_actions"]] != NSNotFound;
}

- (void)requestWritePermissions:(void(^)(BOOL status, NSError *error))callback {
    if (self.hasWritePermissions) {
        callback(YES, nil);
        return;
    }

    if (FBSession.activeSession.isOpen) {
        [FBSession.activeSession
         requestNewPublishPermissions:@[@"publish_actions"]
         defaultAudience:FBSessionDefaultAudienceFriends
         completionHandler:^(FBSession *session, NSError *error) {
             NSLog(@"error = %@", error);
             NSLog(@"session open = %d", session.isOpen);
             NSLog(@"session.permissions = %@", session.permissions);
             NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);

             if (self.hasWritePermissions) {
                 callback(YES, nil);
             }
             else {
                 callback(NO, error);
             }
         }];
    }
    else {
        [FBSession
         openActiveSessionWithPublishPermissions:@[@"publish_actions"]
         defaultAudience:FBSessionDefaultAudienceFriends
         allowLoginUI:YES
         completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
             NSLog(@"error = %@", error);
             NSLog(@"session open = %d", session.isOpen);
             NSLog(@"status = %u", status);
             NSLog(@"session.permissions = %@", session.permissions);
             NSLog(@"session.accessTokenData.declinedPermissions = %@", session.accessTokenData.declinedPermissions);

             [self requestWritePermissions:callback]; // this time, with an open session
         }];
    }
}

如果没有会话,我会运行openActiveSessionWithPublishPermissions,然后再次运行requestNewPublishPermissions

问题是openActiveSessionWithPublishPermissions正在解雇回调,甚至没有去Facebook应用获取更多权限(看起来像FB bug,会报告),但这种方法似乎解决了它。

我发现的另一个问题是session.permissions并不总是反映Facebook上的权限。我发现确保我拥有最新权限的唯一方法是发出API请求:

[FBRequestConnection startWithGraphPath:@"/me/permissions" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

并检查result.data数组是否允许/拒绝权限。

答案 1 :(得分:0)

我对这种方法有类似的问题。我最终以一整套权限(读取和写入)打开会话,它解决了我的问题。

-(void)requestPublishPermissionsWithCompletion:(FBHandlerComp)completion{

if (self.session.isOpen && [self.session.permissions  containsObject:@"publish_actions"]){
    //we have an open session and all neceassarry pemissions
    completion(true,nil);
}else{
    //something in missing, to account to all diffrent scenarios (missing persmission, expired tokens, changes in user sessting etc..), we reinisilise the session and request permissions

      //for publish permissions we need to ask for the whole set( read& publish)
    NSMutableArray *permissions = [NSMutableArray arrayWithArray:self.writePersmissions];
    [permissions addObjectsFromArray:self.readPersmissions];


    self.session = [[FBSession activeSession]initWithAppID:nil permissions:permissions defaultAudience:FBSessionDefaultAudienceFriends urlSchemeSuffix:nil tokenCacheStrategy:nil];

    [self openFacebookSessionWithCompleteion:^(BOOL result, NSError *error) {    
    if (result) {

        completion(true,nil);

    }else{
        if (LOGGING_IS_ON) DebugLog(@"could not get publish permissions- could not open session %@",error);
         completion(false,nil);
    }
}];

}

}