Auto-post to Facebook iOS 6 with SLRequest

Discussion in 'iOS Programming' started by newtoiphonesdk, Jan 7, 2013.

  1. newtoiphonesdk, Jan 7, 2013
    Last edited: Jan 7, 2013

    macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I want my blog app to be able to automatically post to the user's Facebook wall when they view an article. I have the following code in, and the first time it asks for permission to post, but it never actually does. What am I doing wrong?
    Code:
    -(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: @[@"publish_stream", @"publish_actions"],
    ACFacebookAudienceKey: ACFacebookAudienceFriends
        };
    
        [accountStore requestAccessToAccountsWithType:facebookAccountType
                                              options:options completion:^(BOOL granted, NSError *e)
         {
             if (granted)
             {
                 NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
    
                 facebookAccount = [accounts lastObject];
             }
             else
             {
                 // Handle Failure
             }
         }];
    
        NSDictionary *parameters = @{@"message": html};
    
        NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];
    
        SLRequest *feedRequest = [SLRequest
                                  requestForServiceType:SLServiceTypeFacebook
                                  requestMethod:SLRequestMethodPOST
                                  URL:feedURL
                                  parameters:parameters];
    
        feedRequest.account = facebookAccount;
    
        [feedRequest performRequestWithHandler:^(NSData *responseData,
                                                 NSHTTPURLResponse *urlResponse, NSError *error)
         {
             // Handle response
         }];
    }
     
  2. macrumors 6502a

    Joined:
    Mar 30, 2012
    #2
    If you go to post with it, or authorise again, does the FB app tell you:
    "You have already authorised applicationname Press Okay to Continue"?

    I don't claim to be an expert, but I have played around with FB SDK
    with success, and that's the screen I get when my app logs in.
    It occurs to me that otherwise, the app's permission has failed.
     
  3. newtoiphonesdk, Jan 8, 2013
    Last edited: Jan 8, 2013

    thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    EDIT: After more testing, realized have to ask for basic permissions BEFORE asking permission to post. I did this and edited the code to what is below, and get permissions granted, but still not posting.
    Code:
    -(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);
         }];
    }
    
    EDIT:So, the issue before was you can't get permission to post until after you already have permission for basic info. So, I did that and edited the code above to show this. However, still nothing is happening on Facebook. Error in log comes back null for the final part. Any thoughts on this?
     
  4. macrumors 6502a

    Joined:
    Mar 30, 2012
    #4
    You wouldn't have seen that screen if you didn't get permission then.
    I'm using an old FB SDK that still compiles, and don't know much about it,
    but if you get to that screen, it might tell you any problem with your app
    at the FB end.
    To this day it complains my app has no bundle ID because I never filled it
    in, but it does always log in and post.
    I only used a sample to post in a fashion not possible for a Human end user
    with app name "MyFirstName's Real Life!".
     
  5. newtoiphonesdk, Jan 8, 2013
    Last edited: Jan 8, 2013

    thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    Sorry, I had made a couple edits and updates to code since your earlier post and my reply. Did you see the edits? I got the permissions, it no longer asks for permissions, but still won't post message, and no error messages.

    Finally got HTTP response code of 400. Also An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
     

Share This Page