Raphael
Raphael

Reputation: 41

How to upload an NSImage to Facebook-Graph Api (through HTTP POST)

Very first question here... I'm desperately trying to upload a photo to Facebook from my Mac OS App (cocoa), using an HTTP POST request. Using the "https://graph.facebook.com/me/photos?access_token=....." followed by "source=MyURLOnline.jpg", it works great, but I need to upload the DATA, not a link of an image already on the Web...

So I'm switching "feed" to "photos", and the URL by the RAW data of my NSImage (and maybe the "source=" to "image=" ?). But : I set the header of my request to "multipart/form-data, boundary=%AaB03x" and add some "\r\n" and "Content-Type: image/jpeg\r\n\r\n" etc to the body, but the only thing I get is the error "(#324) Requires upload file"...

I have a few years of experience on Cocoa but don't know anything about HTTP request, and especially what is the Graph API expecting, so I've read all Facebook help I found, would have loved to find an example as I'm sure I make several mistakes, but I'm wondering if it is just possible at all.

ANY help appreciated !

UPDATE: Thank you very much Anvesh. If you know that I should POST a JSON then I spent my day trying to figure out how to do that, but no success yet. Here is my code, if I POST a "source=URLOnline.jpg" to the "feed" (and remove the HTTPHeader and the Body), my image shows on my wall. With my image data to the "photos", the only hint I receive is the #324 error... Wondering where I can find out what I should exactly write in the HTTPBody.

// Convert NSImage to data

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];

// [connection start];

// [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

// }];

Upvotes: 0

Views: 879

Answers (1)

Raphael
Raphael

Reputation: 41

Thank you Anvesh, with your help and a Cocoa post about HTTP POST, I finally managed to create my NSURLRequest to upload my NSImage to Facebook. I hope it will save a lot of trouble to people connecting their Mac App to Facebook, as there is no 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.

Upvotes: 1

Related Questions