2013-02-25 92 views
0

这里的第一个问题... 我拼命地尝试从我的Mac OS应用程序(可可)上传一张照片到Facebook,使用HTTP POST请求。 使用“https://graph.facebook.com/me/photos?access_token= .....”后跟“source = MyURLOnline.jpg”,它的效果很好,但我需要上传数据,而不是已经在Web上的图像的链接...如何上传NSImage到Facebook图Graph Api(通过HTTP POST)

所以我将“feed”切换为“照片”,并通过我的NSImage的RAW数据(也可能是“source =”到“image =”?)来切换URL。 但是:我将我的请求头设置为“multipart/form-data,boundary =%AaB03x”并添加了一些“\ r \ n”和“Content-Type:image/jpeg \ r \ n \ r \ n”等等,但我唯一得到的是错误“(#324)需要上传文件”...

我对Cocoa有几年的经验,但对HTTP请求一无所知,特别是Graph API期望的是什么,所以我已经阅读了我发现的所有Facebook帮助,本来很想找到一个例子,因为我确信我犯了几个错误,但是我想知道它是否可能。

任何帮助赞赏!

更新: 非常感谢Anvesh。 如果你知道我应该发布一个JSON,那么我花了我的一天试图找出如何做到这一点,但没有成功。 这里是我的代码,如果我向“feed”发布“source = URLOnline.jpg”(并删除HTTPHeader和Body),我的图像显示在我的墙上。随着我的图片数据到“照片”,我收到的唯一提示是#324错误... 想知道我在哪里可以找到我应该在HTTPBody中准确写入的内容。

//转换NSImage中数据

NSImage * MyIm = [[NSImage alloc] initWithContentsOfURL:[MyLogoPath URL]]; 
NSData *imageData = [MyIm TIFFRepresentation]; 
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 
imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; 


// HTTP Request with access token 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
NSString * MyStr = [@"https://graph.facebook.com/me/photos?access_token=" stringByAppendingString:FacebookToken]; // feed?access_token 

[request setURL:[NSURL URLWithString:MyStr]]; 

[request setHTTPMethod:@"POST"]; 


const char *bytes = [[NSString stringWithFormat:@"&image="] UTF8String]; 

// const char *bytes = [[NSString stringWithFormat:@"&source=http://www.google.ca/intl/en_ALL/images/logos/images_logo_lg.gif"] UTF8String]; 
NSMutableData * MyData = [NSMutableData dataWithBytes:bytes length:strlen(bytes)]; 


// HTTP Header 
NSString * boundary = [NSString stringWithFormat:@"AaB03x"]; 
NSString * contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 

[request addValue:contentType forHTTPHeaderField:@"Content-Type"]; 

// HTTP Body 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[MyData appendData:body]; 
[request setHTTPBody:MyData]; 

[[FacebookView mainFrame] loadRequest:request]; 


// NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:MyURLRequest delegate:self]; 

// [连接开始];

// [NSURLConnection的sendAsynchronousRequest:请求队列:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *响应,NSData的*数据,NSError *错误){

//}];

+0

您可以添加一部分代码来显示您发布的内容吗?当使用Facebook的Graph API库时,图像以“basename($ file)=>'@'。$ file'作为JSON编码数组发送,其中$ file具有文件位置。 – 2013-02-25 08:19:54

+0

在这里,我添加了代码的问题,我想它会更清晰一点(只有一点点...) – Raphael 2013-02-26 07:43:56

+0

我遵循的How-to文章,[这里](https://developers.facebook。 com/blog/post/498 /),并且能够使用'source'作为**文件输入框的'name'成功发布图像**,该文件位于我的本地系统上,表单提交了它登录到Facebook帐户后,登录到'/ me/photos'端点。你可以在技术堆栈中尝试一些相同的东西吗? – 2013-02-28 06:49:51

回答

1

谢谢Anvesh,在您的帮助和关于HTTP POST的Cocoa文章中,我终于设法创建了我的NSURLRequest,将我的NSImage上传到Facebook。 我希望这会为连接他们的Mac App到Facebook的人们节省很多麻烦,因为没有SDK。

// Convert the NSImage to NSData 
NSData *imageData = [MyIm TIFFRepresentation]; 
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 
imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; 

// Create the URL request with the access token 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
NSString * MyStr = [@"https://graph.facebook.com/me/photos?access_token=" stringByAppendingString:FacebookToken]; 

[request setURL:[NSURL URLWithString:MyStr]]; 

[request setHTTPMethod:@"POST"]; 

// Create the header and body of the request with all those settings 
NSMutableData *body = [NSMutableData data]; 
NSString * boundary = [NSString stringWithFormat:@"Random_Boundary_Chars"]; 

NSString *contentType2 = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
[request addValue:contentType2 forHTTPHeaderField:@"Content-Type"]; 

[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\".tiff\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[NSData dataWithData:imageData]]; 
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 

[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[request setHTTPBody:body]; 

// [[FacebookView mainFrame] loadRequest:request]; 

// Send the request, not showing in the webview 
NSData * returnData = [ NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil ]; 
NSString * returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 

NSLog(@"Returned Json: %@", returnString); 

//NSImage is now on Facebook's wall and we got the ID returned.