使用SLRequest将多个图像上传到脸书 [英] upload multiple images to facebook using SLRequest

查看:149
本文介绍了使用SLRequest将多个图像上传到脸书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



SDK Way(工作完美):

  FBSDKSharePhotoContent * content = [[FBSDKSharePhotoContent alloc] init]; 

NSMutableArray * uploadImages = [[NSMutableArray alloc] init];
(UIImage *图像中的图像){
FBSDKSharePhoto * sharePhoto = [[FBSDKSharePhoto alloc] init];
sharePhoto.caption = title;
sharePhoto.image = image;

[uploadImages addObject:sharePhoto];
}
content.photos = [NSArray arrayWithArray:uploadImages];

[FBSDKShareAPI shareWithContent:content delegate:self];

和SLRequest方式(仅发送1张图片):


$ b $ (NSString *)title帐户:(ACAccount *)account completionBlock:(shareSocialResponse)完成
{$($)$ {code
SLRequest * facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:@https://graph.facebook.com/me/photos]
参数:@ {@message:title}];

for(NSData * imageData in imagesData){
[facebookRequest addMultipartData:imageData
withName:@source
type:@multipart / form-data
filename:@photo!];
}
NSLog(@facebookRequest%@,facebookRequest);
facebookRequest.account = account;
[facebookRequest performRequestWithHandler:^(NSData * responseData,NSHTTPURLResponse * urlResponse,NSError * error)
{
if(error){
NSLog(@%@,error描述);

} else {
NSDictionary * returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:& error];
NSLog(@returnedData:%@,[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

if([urlResponse statusCode]!= 200){

dispatch_async(dispatch_get_main_queue(),^ {
NSString * errorMessage = @我们无法处理您的请求现在;
if([ReturnsData valueForKeyPath:@error.message]!= nil){
errorMessage = [returnedData valueForKeyPath:@error.message];
}
完成(NO,errorMessage);
});
} else {
完成(YES,@您的剪辑已发布!);
}
}
}];
}




我相信如果每个数据发送在多部分可能会工作,但不是。


任何人都有任何想法如何做?谢谢。

解决方案

在facebook文档中有一些可能会帮助您的文章。



https ://developers.facebook.com/docs/graph-api/making-multiple-requests



所以,在你的情况下,你需要发送一个在您的请求中称为批处理的字段,每个文件必须附加一个唯一的名称,因此不会被另一个名称重写。



批处理字段的内容将是一个json,您将在其中指定请求的端点,如此(minified btw):

  [
{
method:POST,
relative_url:me / photos ,
body:message = labrys,
attached_files:file1
},
{
method:POST,
relative_url:我/照片,
body:message = circle,
attached_files:file2
}
]

我用邮递员尝试过,该请求的设置应该看起来像这样:





对于不发布代码,我不精通客观C,但希望您能够了解到这一点。


I have 2 ways for share images to Facebook.

The SDK Way (work perfect):

FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];

    NSMutableArray *uploadImages = [[NSMutableArray alloc] init];
    for (UIImage *image in images) {
        FBSDKSharePhoto *sharePhoto = [[FBSDKSharePhoto alloc] init];
        sharePhoto.caption = title;
        sharePhoto.image = image;

        [uploadImages addObject:sharePhoto];
    }
    content.photos = [NSArray arrayWithArray:uploadImages];

    [FBSDKShareAPI shareWithContent:content delegate:self];

and SLRequest way (only send 1 image):

+ (void)uploadPhotoToFacebook:(NSMutableArray *)imagesData imageTitle:(NSString *)title account:(ACAccount*)account completionBlock:(shareSocialResponse)completion
{
    SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                    requestMethod:SLRequestMethodPOST
                                                              URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"]
                                                       parameters:@{@"message": title}];

    for (NSData *imageData in imagesData) {
        [facebookRequest addMultipartData:imageData
                                 withName:@"source"
                                     type:@"multipart/form-data"
                                 filename:@"photo!"];
    }
    NSLog(@"facebookRequest %@",facebookRequest);
    facebookRequest.account = account;
    [facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
    {
        if (error) {
            NSLog(@"%@",error.description);

        } else {
            NSDictionary *returnedData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];
            NSLog(@"returnedData: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);

            if ([urlResponse statusCode] != 200) {

                dispatch_async(dispatch_get_main_queue(), ^{
                    NSString *errorMessage = @"We could not process your request now";
                    if ([returnedData valueForKeyPath:@"error.message"] != nil) {
                        errorMessage = [returnedData valueForKeyPath:@"error.message"];
                    }
                    completion(NO, errorMessage);
                });
            } else {
                completion(YES, @"Your clip was posted!");
            }
        }
    }];
}

I believed that if each data sent in a multipart might work, but no.

Anyone have any idea how to do it? Thanks.

解决方案

There is some article in the facebook docs that might help you.

https://developers.facebook.com/docs/graph-api/making-multiple-requests

So, in your case you will need to send a field called batch in your request and each file must be attached with a unique name so it won't be rewrite by another.

The content of batch field will be a json in which you will specify the endpoint of your request, something like this(minified btw):

[
  {
    "method": "POST",
    "relative_url": "me/photos",
    "body": "message=labrys",
    "attached_files": "file1"
  },
  {
    "method": "POST",
    "relative_url": "me/photos",
    "body": "message=circle",
    "attached_files": "file2"
  }
]

I tried it with postman, the settings for that request should look like this:

Sorry for not posting code, I'm not versed in objective C but I hope you get the idea of this.

这篇关于使用SLRequest将多个图像上传到脸书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆