0

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

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

我在 Cocoa 上有几年的经验,但对 HTTP 请求一无所知,尤其是 Graph API 的期望是什么,所以我已经阅读了我找到的所有 Facebook 帮助,我很想找到一个例子当然我犯了几个错误,但我想知道这是否可能。

任何帮助表示赞赏!

更新:非常感谢 Anvesh。如果您知道我应该发布一个 JSON,那么我花了一天时间试图弄清楚如何做到这一点,但还没有成功。这是我的代码,如果我将“source=URLOnline.jpg”发布到“提要”(并删除 HTTPHeader 和正文),我的图像就会显示在我的墙上。将我的图像数据添加到“照片”中,我收到的唯一提示是 #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:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

// }];

4

1 回答 1

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.
于 2013-03-05T18:44:44.860 回答