Search code examples
facebook-graph-apifacebook-ios-sdk

Change FBSession with page access token to publish post on fb page as admin


I want to publish a post as admin in a facebook page where the user is admin of the page. I have the page access token from

[FBRequestConnection startWithGraphPath:@"/me/accounts"
                                       parameters:nil
                                     HTTPMethod:@"GET"
                              completionHandler:^(
                                                  FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error
                                                  ) {
                              NSString *token = [[[result objectForKey:@"data"] objectAtIndex:0] objectForKey:@"access_token"];//accessToken of the page
}];

Now how can i change the FBSession with this token to publish a post on the page as an admin using GraphAPI? FBDocumentation refers to this for openFromAccessTokenData . Please help as i m stuck with this for long time. I m using facebook sdk 3.2. Thanks in advance


Solution

  • NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:token, @"access_token",
                                                             titleCell.titleTextView.text,@"message",
                                                             [UserDefaultsManager fbPlaceId], @"place",
                                                             //                                          fbPhotoId,@"object_attachment",
                                                             @"https://www.google.com",@"link",
                                                             photoUrl,@"picture",
                                                             titleCell.titleTextView.text,@"name",
                                                             typeCell.cellTextField.text,@"caption",
                                                             descriptionCell.descriptionTextView.text,@"description",
                                                             nil];
    
                                          FBRequest *requestToPost = [[FBRequest alloc] initWithSession:nil
                                                                                        graphPath:@"/me/feed"
                                                                                       parameters:param
                                                                                       HTTPMethod:@"POST"];
    
                                          FBRequestConnection *requestToPostConnection = [[FBRequestConnection alloc] init];
                                          [requestToPostConnection addRequest:requestToPost completionHandler:^(FBRequestConnection *connection, id result, NSError *error){
                                              if(!error)
                                              {
                                                  NSLog(@"facebook result >> %@", result);
    
                                                  NSData *photoData = UIImagePNGRepresentation(promoImage);
    
                                                  NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:token,@"access_token",
                                                                                   photoData,@"source", nil];
    
                                                  FBRequest *requestToPostPhoto = [[FBRequest alloc] initWithSession:nil
                                                                                                 graphPath:@"/me/photos"
                                                                                                parameters:param
                                                                                                HTTPMethod:@"POST"];
    
                                                  FBRequestConnection *requestToPostPhotoConnection = [[FBRequestConnection alloc] init];
                                                  [requestToPostPhotoConnection addRequest:requestToPostPhoto completionHandler:^(FBRequestConnection *connection, id result, NSError *error){
                                                      if(!error)
                                                      {
                                                          [loadingAlert dismissWithClickedButtonIndex:0 animated:YES];
                                                          NSLog(@"facebook result photo>> %@", result);
    
                                                          doneAlert = [[UIAlertView alloc]          initWithTitle:@"Success"
                                                                                                          message:@""
                                                                                                         delegate:self
                                                                                                cancelButtonTitle:@"OK"
                                                                                                otherButtonTitles:nil];
                                                          if(self.isUpdatingPromo)
                                                          {
                                                              doneAlert.message = @"Promo updated successfully";
                                                              [doneAlert show];
                                                          }
                                                          else
                                                          {
                                                              doneAlert.message = @"Promo created successfully";
                                                              [doneAlert show];
                                                          }
                                                      }
                                                      else
                                                      {
                                                          [loadingAlert dismissWithClickedButtonIndex:0 animated:YES];
                                                          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                                          message:@"Could not post photo"
                                                                                                         delegate:nil
                                                                                                cancelButtonTitle:@"Dismiss"
                                                                                                otherButtonTitles:nil];
                                                          [alert show];
                                                      }
                                                  }];
                                                  [requestToPostPhotoConnection start];
                                              }
                                              else
                                              {
                                                  [loadingAlert dismissWithClickedButtonIndex:0 animated:YES];
                                                  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                                                                  message:@"Could not post"
                                                                                                 delegate:nil
                                                                                        cancelButtonTitle:@"Dismiss"
                                                                                        otherButtonTitles:nil];
                                                  [alert show];
                                              }
                                          }];
                                          [requestToPostConnection start];