1
votes

I am trying to post/share a picture on Facebook. First, I am getting publish permissions using:

        NSArray *permissionsNeeded = @[@"publish_actions"];
        [FBRequestConnection startWithGraphPath:@"/me/permissions"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if (!error){
                                      NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                      NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];

                                      for (NSString *permission in permissionsNeeded){
                                          if (![currentPermissions objectForKey:permission]){
                                              [requestPermissions addObject:permission];
                                          }
                                      }

                                      if ([requestPermissions count] > 0){
                                          [FBSession.activeSession requestNewPublishPermissions:requestPermissions
                                                                                defaultAudience:FBSessionDefaultAudienceFriends
                                                                              completionHandler:^(FBSession *session, NSError *error) {
                                                                                  if (!error) {
                                                                                      [self shareDataOnFacebook];
                                                                                  } else {
                                                                                      NSLog(@"%@", error.description);
                                                                                  }
                                                                              }];
                                      } else {
                                          [self shareDataOnFacebook];
                                      }

                                  } else {
                                      NSLog(@"%@", error.description);
                                  }
                              }];

If I NSLog the session, I am getting this:

FBSessionStateOpenTokenExtended, loginHandler: 0x15eab870, appID: 719202928131376, urlSchemeSuffix: , tokenCachingStrategy:, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2014-05-10 12:57:41 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:( status, permission, "publish_actions" )>

Now, If I try to post the picture using:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"{image-url}", @"url",
                       nil
                   ];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/photos"
                         parameters:params
                         HTTPMethod:@"POST"
                  completionHandler:^(
                      FBRequestConnection *connection,
                      id result,
                      NSError *error
                  ) {
                      /* handle the result */
                  }];

I am getting the error:

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x15e4f370 {com.facebook.sdk:HTTPStatusCode=403, com.facebook.sdk:ParsedJSONResponseKey={ body = { error = { code = 200; message = "(#200) Permissions error"; type = OAuthException; }; }; code = 403; }, com.facebook.sdk:ErrorSessionKey=, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2014-05-10 12:57:41 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:( status, permission, "publish_actions" )>}

Even, If when I get the permissions again, "publish_actions" isn't in the list. Please guide me what I am doing wrong.

Is there any other way of sharing/posting just picture with description(without any link, which is required for Share dialogue)?

2

2 Answers

1
votes

After banging my head for hours, I figured it out. It seems you need to go into your app Settings on developers.facebook.com, under "Status & Review", and request publish permissions for your app. If you only need to test it, you can get around by going to Roles and adding the Facebook user you're trying to login with as a developer for your app.

0
votes

This worked for me.

NSArray *requestPermission = @[@"publish_actions"];
        // Open session with public_profile
        [FBSession openActiveSessionWithPublishPermissions:requestPermission defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
             if (!error){
                 // If the session was opened successfully
                 if (state == FBSessionStateOpen){
                     // Your code here
                      NSLog(@"session opened");
                     [self postToFacebook];

                 } else {
                     // There was an error, handle it
                     NSLog(@" error on opening session = %@",error);
                 }
             }
         }];





-(void)postToFacebook{
NSLog(@"post to facebook");
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Sharing Tutorial", @"name",
                               @"Build great social apps and get more installs.", @"caption",
                               @"Allow your users to share stories on Facebook from your app using the iOS SDK.", @"description",
                               @"https://developers.facebook.com/docs/ios/share/", @"link",
                               @"http://i.imgur.com/g3Qc1HN.png", @"picture",
                               nil];

// Make the request
[FBRequestConnection startWithGraphPath:@"/me/feed"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error) {
                              // Link posted successfully to Facebook
                              NSLog(@"result: %@", result);
                          } else {
                              // An error occurred, we need to handle the error
                              // See: https://developers.facebook.com/docs/ios/errors
                              NSLog(@"%@", error.description);
                          }
                      }];

}