Search code examples
iosiphonefacebookfacebook-graph-apifacebook-ios-sdk

Error when post score using social frame work Ios


This my method to send new score:

-(void)sendScore :(int) score : (NSString *)uID : (ACAccount *)ac{
    NSString *url =[NSString stringWithFormat:@"https://graph.facebook.com/%@/scores",uID];
    NSURL * strURL =[NSURL URLWithString:url];
    NSDictionary * parameter =@{@"score": @"10000"};
    SLRequest * request =[SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodPOST URL:strURL parameters:parameter];
    request.account = _accFB;
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if (!error) {
            NSLog(@"Error: %@",error);
            NSString * str = [[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding];
            NSLog(@"str: %@",str);
        }

    }];
}

When run , it show error:

{
    "message":"(#200) Requires extended permission: publish_actions",
    "type":"OAuthException",
    "code":200
}

How I can add publish_actions ?


Solution

  • You can request additional permissions for an active session at any time. Facebook recommends that you ask for permissions when your app actually needs them to complete an action initiated by the user. Also note that you cannot add read and write permissions together.

    Here's the code to request for publish_actions publish permissions for an active session-

    FB iOS SDK

    [FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:@"publish_actions"]
                                      defaultAudience:FBSessionDefaultAudienceFriends
                                    completionHandler:^(FBSession *session, NSError *error) {
                                      __block NSString *alertText;
                                      __block NSString *alertTitle;
                                      if (!error) {
                                        if ([FBSession.activeSession.permissions 
                                             indexOfObject:@"publish_actions"] == NSNotFound){
                                          // Permission not granted, tell the user we will not publish
                                          alertTitle = @"Permission not granted";
                                          alertText = @"Your action will not be published to Facebook.";
                                          [[[UIAlertView alloc] initWithTitle:title
                                                          message:text
                                                         delegate:self
                                                cancelButtonTitle:@"OK!"
                                                otherButtonTitles:nil] show];
                                        } else {
                                          // Permission granted, publish the OG story
                                          [self publishStory];
                                        }
    
                                      } else {
                                        // There was an error, handle it
                                        // See https://developers.facebook.com/docs/ios/errors/
                                      }
                                    }];
    

    Social Framework

    -(void)requestPermissions
    {
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];
        __block ACAccount *facebookAccount = nil;
    
        ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    
        // Specify App ID and permissions
        NSDictionary *options = @{
                        ACFacebookAppIdKey: @"MYAPPID",
                        // READ permissions here (if any)
                        ACFacebookPermissionsKey: @[@"email"],
                        ACFacebookAudienceKey: ACFacebookAudienceFriends
       };
    
       [accountStore requestAccessToAccountsWithType:facebookAccountType
                                           options:options completion:^(BOOL granted, NSError *e)
       {
         if (granted) {
    
             NSDictionary *options2 = @{
                              ACFacebookAppIdKey: @"MYAPPID",
                              // PUBLISH permissions here
                              ACFacebookPermissionsKey: @[@"publish_actions"],
                              ACFacebookAudienceKey: ACFacebookAudienceFriends
             };
             [accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
                 if (granted) {
                     NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
    
                     facebookAccount = [accounts lastObject];                 
                 }
                 else {
                     NSLog(@"Access denied 2");
                     NSLog(@"%@", [error description]);
                 }
             }];
         } else {
             NSLog(@"Error: %@", [e description]);
             NSLog(@"Access denied");
         }
     }];
    }