问题描述
我的一个应用程序是上传视频到Facebook帐户。
我查了网站,却发现大部分的解决方案是旧的或删除。
是否有任何更新的解决方案?
One of my app is to upload video to facebook account.I checked on web, but found that most of solution are old or removed.Is there any updated solution?
欢迎任何评论
推荐答案
在您可以发布到Facebook上,你必须得到发布(写)的权限,无论是使用原生集成或Facebook的SDK,规则是你必须首先前写权限获得读取权限。
Context
Before you can publish onto Facebook, you must get publish (write) permissions, using either native integration or the Facebook SDK, the rule is you must first acquire read permissions before write permissions.
因此,请确保您尝试上传视频之前,您应该已经基本请求信息(电子邮件为例),那么,一旦你有了这个,你可以要求写权限。上传视频所需的权限是 publish_stream
。
Thus, make sure that before you attempt to upload a video, you should have requested basic info (email for example), then, once you have this, you can request write permissions. The permission necessary for uploading videos is publish_stream
.
使用原生的iOS 6 Facebook的整合,你应该使用 requestForServiceType:requestMethod:网址:参数:
SLRequest
的方法,如下所示:
Using the native iOS 6 Facebook integration, you should use the requestForServiceType:requestMethod:URL:parameters:
method of SLRequest
, as follows:
- (void)upload{
NSURL *videourl = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *params = @{
@"title": @"Me being silly",
@"description": @"Me testing the video upload to Facebook with the new system."
};
SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:videourl
parameters:params];
[uploadRequest addMultipartData:videoData
withName:@"source"
type:@"video/quicktime"
filename:[pathURL absoluteString]];
uploadRequest.account = self.facebookAccount;
[uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if(error){
NSLog(@"Error %@", error.localizedDescription);
}else
NSLog(@"%@", responseString);
}];
}
这里要说明的是,视频数据并没有进入参数字典中是很重要的,则必须使用被添加到 SLRequest
对象 addMultipartData :withName:类型:文件名:
法
另外请注意,添加的影片数据时的文件名是非常重要的。在这里,我只是用文件的完整路径。
Also note that the filename is very important when adding the videos data. Here I am just using the full path of the file.
如果您必须支持的iOS版本早那么iOS 6中,或者您希望使用Facebook SDK 3.1任何其他原因,上传视频是一个有点不同。
If you must support iOS versions earlier then iOS 6 or you wish to use the Facebook SDK 3.1 for any other reason, uploading a video is a little different.
您必须使用 FBRequest
对象和的NSDictionary
包含视频的详情。我建议使用的方法是 requestWithGraphPath:参数:列举HTTPMethod:
,我用这个方法了preference的虽然你应该能够使用一些其他的方法创建请求对象。
You must use a FBRequest
object and a NSDictionary
that contains the videos details. The method I recommend using is requestWithGraphPath:parameters:HTTPMethod:
, I've used this method out of preference although you should be able to use some of the other methods to create your request object.
以下code与Facebook SDK 3.1的作品上传一个视频:
The following code works with Facebook SDK 3.1 to upload a video:
- (void)upload{
if (FBSession.activeSession.isOpen) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSDictionary *videoObject = @{
@"title": @"FB SDK 3.1",
@"description": @"hello there !",
[pathURL absoluteString]: videoData
};
FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos"
parameters:videoObject
HTTPMethod:@"POST"];
[uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error)
NSLog(@"Done: %@", result);
else
NSLog(@"Error: %@", error.localizedDescription);
}];
}
}
在这里,你可以看到,我们添加了视频数据到参数
字典不同的是previous的解决方案,它的存在与标题沿
和说明
(其中2个可选参数)。
还要注意的是,这里没有钥匙源
,如Facebook的文件规定。
关键的名字是视频的文件名。我不知道为什么这不应该是源
,而在 com.facebook.sdk错误使用源代码结果5
Here as you can see, we add the videos data into the parameters
dictionary unlike the previous solution, it's there along with title
and description
(which are 2 optional parameters).Also note that here there is no key source
, as specified by the Facebook documentation.The key's name is the filename of the video. I don't know why this shouldn't be source
, but using source results in a com.facebook.sdk error 5.
我提到我申请与Facebook的错误,你可以看到这个报告 - 除非我错了在文档的我间pretation。请尝试这个bug并报告,如果你能重现它。谢谢!
The bug I mentioned I filed with Facebook, you can see this report on this link - unless I'm mistaken in my interpretation of the documentation. Please try out that bug and report if you can reproduce it. Thanks !
这篇关于使用Facebook SDK 3.1在iOS 6.0将视频上传至Facebook的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!