繁体   English   中英

iOS使用Facebook graph API发布没有共享对话框的帖子?

[英]iOS using Facebook graph API for post without share dialog?

我想使用Facebook graph API通过我的应用程序在Facebook上共享,而不显示共享对话框。但是在阅读互联网上的各种线程时,我真的很困惑如何实现这一目标。 我在堆栈溢出时发现了这篇文章 ,但无法找到使它起作用的方法。

任何人都可以给我逐步指南或源代码。 任何帮助将不胜感激。

编辑:我到目前为止使用的代码-

- (IBAction)StatusUpdateWithAPICalls:(id)sender {

    [self openSessionForReadPermissions];
}
- (void)openSessionForReadPermissions
{
    [FBSession openActiveSessionWithReadPermissions:nil
                                       allowLoginUI:YES
                                  completionHandler:
     ^(FBSession *session,
       FBSessionState state, NSError *error) {

         //this is called even from the reauthorizeWithPublishPermissions
         if (state == FBSessionStateOpen && !error)
         {
             //[self openSessionForPublishPermissions];
             //dispatch_async(dispatch_get_current_queue(), ^{
                 [self openSessionForPublishPermissions];
             //});
         }
         else if (state == FBSessionStateClosedLoginFailed)
         {
             [FBSession.activeSession closeAndClearTokenInformation];

           //  [[NSNotificationCenter defaultCenter] postNotificationName:FBLoginErrorNotification object:session];
         }
     }];
}
-(void)openSessionForPublishPermissions
{
    // We will post on behalf of the user, these are the permissions we need:
    NSArray *permissionsNeeded = @[@"publish_actions"];

    // Request the permissions the user currently has
    [FBRequestConnection startWithGraphPath:@"/me/permissions"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if (!error){
                                  // Parse the list of existing permissions and extract them for easier use
                                  NSMutableArray *currentPermissions = [[NSMutableArray alloc] init];
                                  NSArray *returnedPermissions = (NSArray *)[result data];
                                  for (NSDictionary *perm in returnedPermissions) {
                                      if ([[perm objectForKey:@"status"] isEqualToString:@"granted"]) {
                                          [currentPermissions addObject:[perm objectForKey:@"permission"]];
                                      }
                                  }

                                  // Build the list of requested permissions by starting with the permissions
                                  // needed and then removing any current permissions
                                  NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:permissionsNeeded copyItems:YES];
                                  [requestPermissions removeObjectsInArray:currentPermissions];

                                  NSLog(@"Asking: %@", requestPermissions);

                                  // If we have permissions to request
                                  if ([requestPermissions count] > 0){
                                      // Ask for the missing permissions
                                      [FBSession.activeSession requestNewPublishPermissions:requestPermissions
                                                                            defaultAudience:FBSessionDefaultAudienceFriends
                                                                          completionHandler:^(FBSession *session, NSError *error) {
                                                                              if (!error) {
                                                                                  // Permission granted, we can request the user information
                                                                                  [self makeRequestToUpdateStatus];
                                                                              } else {
                                                                                  // An error occurred, handle the error
                                                                                                                                                          NSLog(@"%@", error.description);
                                                                              }
                                                                          }];
                                  } else {
                                      // Permissions are present, we can request the user information
                                      [self makeRequestToUpdateStatus];
                                  }

                              } else {
                                  // There was an error requesting the permission information
                                  // See our Handling Errors guide: https://developers.facebook.com/docs/ios/errors/
                                  NSLog(@"%@", error.description);
                              }
                          }];
}

- (void)makeRequestToUpdateStatus {

    // NOTE: pre-filling fields associated with Facebook posts,
    // unless the user manually generated the content earlier in the workflow of your app,
    // can be against the Platform policies: https://developers.facebook.com/policy

    [FBRequestConnection startForPostStatusUpdate:@"User-generated status update."
                                completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                    if (!error) {
                                        // Status update 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);
                                    }
                                }];
}

但是我得到这个错误-

Error Domain=com.facebook.sdk Code=2 "The operation couldn’t be completed. com.facebook.sdk:ErrorReauthorizeFailedReasonUserCancelled" UserInfo=0x78788e60 {com.facebook.sdk:ErrorLoginFailedReason=com.facebook.sdk:ErrorReauthorizeFailedReasonUserCancelled, NSLocalizedFailureReason=com.facebook.sdk:ErrorReauthorizeFailedReasonUserCancelled, com.facebook.sdk:ErrorSessionKey=<FBSession: 0x787ac750, state: FBSessionStateOpen, loginHandler: 0x787ac710, appID: 4201XXXXXXXXXXX, urlSchemeSuffix: , tokenCachingStrategy:<FBSessionTokenCachingStrategy: 0x7a1c1b50>, expirationDate: 2015-04-20 06:48:00 +0000, refreshDate: 2015-02-19 09:39:47 +0000, attemptedRefreshDate: 0000-12-30 00:00:00 +0000, permissions:(
    "public_profile"
)

而且我不确定我是否做对了。 请建议我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM