Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SLRequest in iOS 6 to Post to Facebook

Trying to get my app to make a simple post to Facebook using SLRequest, but running into issues. Permissions get granted fine, it just won't post.

-(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",
ACFacebookPermissionsKey: @[@"email", @"user_about_me", @"user_likes"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
    };

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

             NSDictionary *options2 = @{
         ACFacebookAppIdKey: @"MYAPPID",
         ACFacebookPermissionsKey: @[@"publish_stream", @"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");
         }
     }];

    NSDictionary *parameters = @{@"message": @"This is a test"};

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

    SLRequest *feedRequest = [SLRequest
                              requestForServiceType:SLServiceTypeFacebook
                              requestMethod:SLRequestMethodPOST
                              URL:feedURL
                              parameters:parameters];
    NSLog(@"AnythingHere?");
    feedRequest.account = facebookAccount;

    [feedRequest performRequestWithHandler:^(NSData *responseData,
                                             NSHTTPURLResponse *urlResponse, NSError *error)
     {
         // Handle response
         NSLog(@"%@%@%@", error, responseData, urlResponse);
     }];
}

Error in log comes back null for the final part. Any thoughts on this?

UPDATE: So the issue is HTTP Response is coming back as 400.

like image 262
user717452 Avatar asked Dec 05 '25 14:12

user717452


1 Answers

You should have made a copy/paste error.

The options have been instanciated two times as well as the two requestAccessToAccountsWithType method that you interlocked.

This looks very recurrent and improbable:

NSDictionary *options = @{...};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *e) {
    if (granted) {
         NSDictionary *options2 = @{...};
         [accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
             if (granted)
                 ...
             else
                ...
        }];
    } else {
        ...
    }
}];

You should start again from the beginning.

like image 94
Stéphane Bruckert Avatar answered Dec 07 '25 05:12

Stéphane Bruckert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!