3

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.

user717452
  • 33
  • 14
  • 73
  • 149

3 Answers3

2

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.

Community
  • 1
  • 1
Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
  • The first one gives access to basic info which is required before access to post to the wall can be given. That is what the 2nd one does. – user717452 Jan 08 '13 at 18:27
  • I was able to retrieve this error finally: `An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}` – user717452 Jan 08 '13 at 18:36
  • What *basic info* are you talking about? You don't need additional prerequisites to use the `/me/feed` query. Can you explain how you think it works? – Stéphane Bruckert Jan 08 '13 at 18:55
  • If I simply ask for publish_stream, I get this message: The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time. – user717452 Jan 09 '13 at 00:48
2

Maybe a bit late... however, it looks like you have to make your feedRequest into the block where you have requested the publish permissions. Something like this :

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

    ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    // Specify App ID and permissions
    NSDictionary *options = @{
                              ACFacebookAppIdKey: @"yourAppId",

                              ACFacebookPermissionsKey: @[@"email"]
                              ACFacebookAudienceKey: ACFacebookAudienceEveryone
                              }; // basic read permissions

    [accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *e)
     {
         if (granted) {
             // Now that you have publish permissions execute the request 
             NSDictionary *options2 = @{
                                        ACFacebookAppIdKey: @"yourAppId",
                                        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];

                     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 setAccount:facebookAccount];

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

                          NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

                          NSLog(@"Facebook Response : %@",response);

                      }];



                 }
                 else {
                     NSLog(@"Access denied 2");
                     NSLog(@"%@", [error description]);
                 }
             }];
         } else {
             NSLog(@"Error: %@", [e description]);
             NSLog(@"Access denied");
         }
     }];
  • Yes, you are right. Even if you login in your app using ACAccountStore, as soon as you publish in the feed you must re-obtain the credentials again by requesting the ACAccount to the ACAccountStore. Of course if you acquired the permission earlier they will not be requested again so for the user the operation is transparent. – viggio24 Jun 25 '14 at 12:35
0

Swift 3

let account = ACAccountStore()
let accountType = account.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
let options: [AnyHashable : Any] = [ACFacebookAppIdKey: "Your App ID on FB", ACFacebookPermissionsKey: ["publish_stream", "publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceEveryone]

account.requestAccessToAccounts(with: accountType, options: options) { (success, error)  in
  if success {
    if let accounts = account.accounts(with: accountType) {
      if accounts.isEmpty {
        print("No facebook account found, please add your facebook account in phone settings")
      } else {
        let facebookAccount = accounts.first as! ACAccount

        let message = ["status": "My first Facebook posting "]
        let requestURL = URL(string: "https://graph.facebook.com/me/feed")

        let postRequest = SLRequest(forServiceType: SLServiceTypeFacebook,
                                    requestMethod: SLRequestMethod.POST,
                                    url: requestURL,
                                    parameters: message)
        postRequest?.account = facebookAccount

        postRequest?.perform(handler: {(_, urlResponse,
          error) in

          if let err = error {
            print("Error : \(err.localizedDescription)")
          }
          print("Facebook HTTP response \(String(describing: urlResponse?.statusCode))")
        })
      }
    }
  } else {
    print("Facebook account error: \(String(describing: error))")
  }
}
Vinoth Anandan
  • 1,157
  • 17
  • 15