第一个问题... 我正在拼命尝试使用 HTTP POST 请求从我的 Mac OS 应用程序 (cocoa) 将照片上传到 Facebook。 使用“https://graph.facebook.com/me/photos?access_token= .....”后跟“source=MyURLOnline.jpg”,效果很好,但我需要上传数据,而不是网络上已有图像的链接...
所以我将“feed”切换为“photos”,并将 URL 切换为 NSImage 的 RAW 数据(也许将“source=”切换为“image=”?)。 但是:我将请求的 header 设置为“multipart/form-data,boundary=%AaB03x”并添加一些“\r\n”和“Content-Type: image/jpeg\r\n\r\n”等到正文,但我得到的唯一结果是错误“(#324) 需要上传文件”...
我在 Cocoa 上有几年的经验,但对 HTTP 请求一无所知,尤其是 Graph API 期望什么,所以我阅读了我找到的所有 Facebook 帮助,很想找到一个示例,如下所示我确信我犯了一些错误,但我想知道这是否可能。
感谢任何帮助!
更新: 非常感谢安维什。 如果您知道我应该 POST 一个 JSON,那么我花了一天的时间试图找出如何做到这一点,但还没有成功。 这是我的代码,如果我将“source=URLOnline.jpg”发布到“feed”(并删除 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:请求队列:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
//}];
最佳答案
谢谢 Anvesh,在您的帮助和关于 HTTP POST 的 Cocoa 帖子下,我终于成功创建了 NSURLRequest 以将我的 NSImage 上传到 Facebook。 我希望它能为人们将 Mac 应用程序连接到 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.
关于cocoa - 如何将 NSImage 上传到 Facebook-Graph Api(通过 HTTP POST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15061789/