Ios 如何在facebook SDK中共享视频?

Ios 如何在facebook SDK中共享视频?,ios,facebook,video,Ios,Facebook,Video,我在参考资料中文件所在的位置编写了如下代码。它不是空的 我成功地添加了图像,但仍停留在视频上 -(void)uploadVideo { NSLog(@"UPload Videio "); NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp4"]; NSError *error = nil; NSData *data = [NSData dataWit

我在参考资料中文件所在的位置编写了如下代码。它不是空的

我成功地添加了图像,但仍停留在视频上

-(void)uploadVideo {

    NSLog(@"UPload Videio ");

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"mp4"];

    NSError *error = nil;


    NSData *data = [NSData dataWithContentsOfFile:filePath options:NSDataReadingUncached error:&error];

    if(data == nil && error!=nil) {
        //Print error description

        NSLog(@"error is %@", [error description]);
    }

    NSLog(@"data is %@", data);

    NSDictionary *parameters = [NSDictionary dictionaryWithObject:data forKey:@"sample.mov"];

    if (FBSession.activeSession.isOpen) {


        [FBRequestConnection startWithGraphPath:@"me/videos"
                                     parameters:parameters
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                //  [FBRequestConnection setVideoMode:NO];

                                  if(!error) {
                                      NSLog(@"OK: %@", result);
                                  } else
                                      NSLog(@"Error: %@", error.localizedDescription);

                              }];

    } else {

        // We don't have an active session in this app, so lets open a new
        // facebook session with the appropriate permissions!

        // Firstly, construct a permission array.
        // you can find more "permissions strings" at http://developers.facebook.com/docs/authentication/permissions/
        // In this example, we will just request a publish_stream which is required to publish status or photos.

        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"publish_stream",
                                nil];
        //[self controlStatusUsable:NO];
        // OPEN Session!
        [FBSession openActiveSessionWithPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                      // if login fails for any reason, we alert
                                      if (error) {

                                          // show error to user.

                                      } else if (FB_ISSESSIONOPENWITHSTATE(status)) {

                                          // no error, so we proceed with requesting user details of current facebook session.


                                          [FBRequestConnection startWithGraphPath:@"me/videos"
                                                                       parameters:parameters
                                                                       HTTPMethod:@"POST"
                                                                completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                                    //  [FBRequestConnection setVideoMode:NO];

                                                                    if(!error) {
                                                                        NSLog(@"Result: %@", result);
                                                                    } else
                                                                        NSLog(@"ERROR: %@", error.localizedDescription);

                                                                }];





                                          //[self promptUserWithAccountNameForUploadPhoto];
                                      }
                                      // [self controlStatusUsable:YES];
                                  }];
    }

}
作为回报,我得到的错误如下

The operation couldn’t be completed. (com.facebook.sdk error 5.)
如何使用facebook iOS SDK将视频上传到facebook


谢谢

推荐:

我认为这可能是一个权限问题,但我不确定错误抛出的位置。将抛出的委托方法未显示在代码中。我认为将代码与本文中的步骤协调起来可能会有所帮助;如果是,请接受答案

样本的一些关键方面:

权限:

- (IBAction)buttonClicked:(id)sender {
    NSArray* permissions = [[NSArray alloc] initWithObjects:
                            @"publish_stream", nil];
    [facebook authorize:permissions delegate:self];
    [permissions release];
}
生成请求:

- (void)fbDidLogin {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mov"];
    NSData *videoData = [NSData dataWithContentsOfFile:filePath];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   videoData, @"video.mov",
                                   @"video/quicktime", @"contentType",
                                   @"Video Test Title", @"title",
                                   @"Video Test Description", @"description",
                       nil];
    [facebook requestWithGraphPath:@"me/videos"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
}
请求加载委托方法:

- (void)request:(FBRequest *)request didLoad:(id)result {
    if ([result isKindOfClass:[NSArray class]]) {
        result = [result objectAtIndex:0];
    }
    NSLog(@"Result of API call: %@", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}
请求失败委托方法:

- (void)request:(FBRequest *)request didLoad:(id)result {
    if ([result isKindOfClass:[NSArray class]]) {
        result = [result objectAtIndex:0];
    }
    NSLog(@"Result of API call: %@", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
    NSLog(@"Failed with error: %@", [error localizedDescription]);
}

Facebook视频权限

您以前请求过
发布流
权限吗?

以下是将视频上传到Facebook的方法。此代码正在测试并100%正常工作

ACAccountStore *accountStore = [[ACAccountStore alloc] init];

ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// Specify App ID and permissions
NSDictionary *options = @{ACFacebookAppIdKey: FACEBOOK_ID,
                          ACFacebookPermissionsKey: @[@"publish_stream", @"video_upload"],
                          ACFacebookAudienceKey: ACFacebookAudienceFriends}; // basic read permissions

[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *e) {
    if (granted) {
        NSArray *accountsArray = [accountStore accountsWithAccountType:facebookAccountType];

        if ([accountsArray count] > 0) {
            ACAccount *facebookAccount = [accountsArray objectAtIndex:0];


            NSDictionary *parameters = @{@"description": aMessage};


            SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                            requestMethod:SLRequestMethodPOST
                                                                      URL:[NSURL URLWithString:@"https://graph.facebook.com/me/videos"]
                                                               parameters:parameters];

            [facebookRequest addMultipartData: aVideo
                                     withName:@"source"
                                         type:@"video/mp4"
                                     filename:@"video.mov"];

            facebookRequest.account = facebookAccount;


            [facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
                if (error == nil) {
                    NSLog(@"responedata:%@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
                }else{
                    NSLog(@"%@",error.description);
                }
            }];
        }
    } else {
        NSLog(@"Access Denied");
        NSLog(@"[%@]",[e localizedDescription]);
    }
}];

此代码在


推荐:In.plist

设置FacebookAppID、FacebookDisplayName,
URL类型->项目0->URL方案设置为带有
fb

-(void)shareOnFaceBook
{
    //sample_video.mov is the name of file
    NSString *filePathOfVideo = [[NSBundle mainBundle] pathForResource:@"sample_video" ofType:@"mov"];

    NSLog(@"Path  Of Video is %@", filePathOfVideo);
    NSData *videoData = [NSData dataWithContentsOfFile:filePathOfVideo];
    //you can use dataWithContentsOfURL if you have a Url of video file
    //NSData *videoData = [NSData dataWithContentsOfURL:shareURL];
    //NSLog(@"data is :%@",videoData);
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               videoData, @"video.mov",
                               @"video/quicktime", @"contentType",
                               @"Video name ", @"name",
                               @"description of Video", @"description",
                               nil];

   if (FBSession.activeSession.isOpen)
   {
        [FBRequestConnection startWithGraphPath:@"me/videos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if(!error)
                              {
                                  NSLog(@"RESULT: %@", result);
                                  [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];
                              }
                              else
                              {
                                  NSLog(@"ERROR: %@", error.localizedDescription);
                                  [self throwAlertWithTitle:@"Denied" message:@"Try Again"];
                              }
                          }];
    }
    else
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"publish_actions",
                            nil];
        // OPEN Session!
        [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone  allowLoginUI:YES
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
                                         if (error)
                                         {
                                             NSLog(@"Login fail :%@",error);
                                         }
                                         else if (FB_ISSESSIONOPENWITHSTATE(status))
                                         {
                                             [FBRequestConnection startWithGraphPath:@"me/videos"
                                                                          parameters:params
                                                                          HTTPMethod:@"POST"
                                                                   completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                                       if(!error)
                                                                       {
                                                                           [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];

                                                                           NSLog(@"RESULT: %@", result);
                                                                       }
                                                                       else
                                                                       {
                                                                           [self throwAlertWithTitle:@"Denied" message:@"Try Again"];

                                                                           NSLog(@"ERROR: %@", error.localizedDescription);
                                                                       }

                                                                   }];
                                         }
                                     }];
        }
}
我得到了一个错误:

 The operation couldn’t be completed. (com.facebook.sdk error 5.)
它发生在facebook被初始化时。下次我打开我的应用程序时,它工作正常,总是第一次。在应用程序中尝试了一切,但似乎都是在Facebook SDK方面

查看
com.facebook.sdk错误5的原因不多:

  • 会话未打开。验证
  • Facebook检测到您正在向系统发送垃圾邮件。更改视频名称
  • Facebook使用SDK有一个已定义的限制。尝试其他应用程序

检查FB推荐的视频格式。从哪里可以只传递链接?他们推荐mp4。请使用新的facebook SDK在此处检查,权限已被替换为
发布操作
,请删除此答案/问题。
发布流
视频上传
不再存在。