Wonton
Wonton

Reputation: 1143

Facebook - ios integration (Social Framework) Invalid permission

I'm working on two different iOS apps:

So I'm doing anything wrong despite I've tried to follow all kind of posts, docs, and so on.

I've specially followed this post iOS 6 Facebook posting procedure ends up with "remote_app_id does not match stored id" error, it has a very good answer.

I think I have created my apps in Facebook Developers ok because I had problems (wrong iPhone ID and error 7) and following the post I've mentioned these problems dissapeared.

When a user clicks on share on facebook I do this:

- (void) loginWithFacebookWithReadPermissionsWithDelegate:(id<IfcRRSSDelegate>)_delegate
{
    self.delegateFB = _delegate;

    ACAccountStore *account = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [cuenta accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSString *facebookKey = MY_FACEBOOK_KEY;
    NSMutableArray *permimssions = [[NSMutableArray alloc] init];
    [permimssions addObject:@"email"];

    self.fbPermission = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                            facebookKey, ACFacebookAppIdKey,
                            ACFacebookAudienceFriends, ACFacebookAudienceKey,
                            permimssions, ACFacebookPermissionsKey, 
                            nil];

    [account requestAccessToAccountsWithType:accountType options:self.fbPermission completion:
     ^(BOOL granted, NSError *error) {
         if(granted)
         {
             NSArray *accounts = [account accountsWithAccountType: accountType];
             ACAccount *_facebookAccount = [accounts lastObject];
             self.accountFB = _facebookAccount;
             ACAccountCredential *credentials = [self.accountFB credential];
             self.tokenFB = [credentials oauthToken];
         }
         else
         {
            // Show error 
         }
     }];
}

If this login goes well (in fact it always goes well) I call this:

- (void) loginWithFacebookWithWritePermissionsWithDelegate:(id<IfcRRSSDelegate>)_delegate
{
    self.delegateFB = _delegate;

    ACAccountStore *account = [[ACAccountStore alloc]init];
    ACAccountType *accountType = [cuenta accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSString *facebookKey = MY_FACEBOOK_KEY;
    NSMutableArray *permimssions = [[NSMutableArray alloc] init];
    [permimssions addObject:@"publish_stream"];
    [permimssions addObject:@"publish_actions"];

    self.fbPermission = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                            facebookKey, ACFacebookAppIdKey,
                            ACFacebookAudienceFriends, ACFacebookAudienceKey,
                            permimssions, ACFacebookPermissionsKey, 
                            nil];

    [account requestAccessToAccountsWithType:accountType options:self.fbPermission completion:
     ^(BOOL granted, NSError *error) {
         NSLog(@">>>>ERROR: %@", error);
         if(granted)
         {
             NSArray *accounts = [account accountsWithAccountType: accountType];
             ACAccount *_facebookAccount = [accounts lastObject];
             self.accountFB = _facebookAccount;
             ACAccountCredential *credentials = [self.accountFB credential];
             self.tokenFB = [credentials oauthToken];
         }
         else
         {
            // Show error 
         }
     }];
}

This second login FAILS, I get this error:

ERROR: Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: Invalid permission: publish_stream" UserInfo=0xc18d700 {NSLocalizedDescription=The Facebook server could not fulfill this access request: Invalid permission: publish_stream}

Invalid permission: publish_stream??? I can hardly find information about this error.

I've tried to comment this permission and have only publish_action, but when I try to post I get this new error:

{
    code = 200;
    message = "(#200) The user hasn't authorized the application to perform this action";
    type = OAuthException;
}

If this second login went well I would call this code to post (but it's never called because the second login fails):

- (void) postWithFacebookWithMessage:(NSString *)msg
{
    if(self.accountFB)
    {
        NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
        [parameters setObject: self.accountFB.credential.oauthToken forKey:@"access_token"];
        [parameters setObject:msg forKey:@"message"];

        NSURL *requestUR = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                requestMethod:SLRequestMethodPOST
                                                          URL:requestURL
                                                   parameters:parameters];
        request.account = self.accountFB;
        [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
            if(!error)
            {
                NSDictionary *list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                if([list objectForKey:@"error"] != nil)
                {
                    // Try to attempt Renew Credentials
                }
                else
                {
                    // Everything works
                }
                dispatch_async(dispatch_get_main_queue(),^{});
            }
            else
            {
                    // Try to attempt Renew Credentials
            }
        }];
    }
}

If this is important, I've checked in my facebook page, in my activity, apps, this app and it sais "This app can publish in your name for: Only you".

Any idea of what is it happening?

Kind regards!

EDIT: Sorry, I made an important mistake explaining the error so I edited the post: the facebook call that fails is the second login, not the post.

Upvotes: 1

Views: 2869

Answers (1)

Vladislav Kovalyov
Vladislav Kovalyov

Reputation: 803

Try this code, which I using in my app:

__block ACAccount * facebookAccount;
        ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    NSDictionary *emailReadPermisson = @{
                                         ACFacebookAppIdKey : @"0123456789",
                                         ACFacebookPermissionsKey : @[@"email"],
                                         ACFacebookAudienceKey : ACFacebookAudienceEveryone,
                                         };

    NSDictionary *publishWritePermisson = @{
                                            ACFacebookAppIdKey : @"0123456789",
                                            ACFacebookPermissionsKey : @[@"publish_actions"],
                                            ACFacebookAudienceKey : ACFacebookAudienceEveryone
                                            };

    ACAccountType *facebookAccountType = [accountStore
                                          accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    //Request for Read permission

    [accountStore requestAccessToAccountsWithType:facebookAccountType options:emailReadPermisson completion:^(BOOL granted, NSError *error) {

        if (granted) {

            NSLog(@"Granted for read");

            //Request for write permission
            [accountStore requestAccessToAccountsWithType:facebookAccountType options:publishWritePermisson completion:^(BOOL granted, NSError *error) {

                if (granted) {

                    NSLog(@"Granded for post");

                    NSArray *accounts = [accountStore
                                         accountsWithAccountType:facebookAccountType];
                    facebookAccount = [accounts lastObject];
                    NSLog(@"Successfull access for account: %@", facebookAccount.username);
                    /* Do any things here */
                }
                else
                {
                    NSLog(@"Access to Facebook is not granted");

                    // Fail gracefully...
                    NSLog(@"%@",error.description);
                }
            }];
        }else{
             /* Error */
        }
    }];

This sample was found in this post

And if you're getting 'not granted' state of your account - check does your app not blocked in Settings->Facebook. If it is - ask user to give access from settings.

Upvotes: 5

Related Questions