Cocoa 如何将NSImage上载到Facebook Graph Api(通过HTTP POST)
这里的第一个问题。。。 我正在拼命尝试使用HTTP POST请求从我的Mac OS应用程序(cocoa)上传一张照片到Facebook。 使用“…..”后跟“source=myurlnonline.jpg”,效果很好,但我需要上传数据,而不是已经在网上的图像链接 因此,我将“feed”切换为“photos”,并根据NSImage的原始数据(可能还有“source=”到“image=?”)创建URL。 但是:我将请求的标题设置为“multipart/form data,boundary=%AaB03x”,并在正文中添加了一些“\r\n”和“Content Type:image/jpeg\r\n\r\n”等,但我得到的唯一错误是“(#324)需要上载文件” 我在Cocoa方面有几年的经验,但对HTTP请求一无所知,尤其是Graph API的期望值是什么,所以我已经阅读了所有Facebook帮助,我很想找到一个例子,因为我确信我犯了几个错误,但我想知道这是否可能 感谢您的帮助 更新: 非常感谢安维斯。 如果你知道我应该发布一个JSON,那么我花了一天的时间试图找出如何做到这一点,但还没有成功。 这是我的代码,如果我将“source=urlnonline.jpg”发布到“提要”(并删除HTTPHeader和主体),我的图像就会显示在我的墙上。将我的图像数据添加到“照片”中,我收到的唯一提示是#324错误。。。 想知道在哪里可以找到我应该在HTTPBody中确切地写些什么 //将NSImage转换为数据Cocoa 如何将NSImage上载到Facebook Graph Api(通过HTTP POST),cocoa,http,facebook-graph-api,post,nsurlrequest,Cocoa,Http,Facebook Graph Api,Post,Nsurlrequest,这里的第一个问题。。。 我正在拼命尝试使用HTTP POST请求从我的Mac OS应用程序(cocoa)上传一张照片到Facebook。 使用“…..”后跟“source=myurlnonline.jpg”,效果很好,但我需要上传数据,而不是已经在网上的图像链接 因此,我将“feed”切换为“photos”,并根据NSImage的原始数据(可能还有“source=”到“image=?”)创建URL。 但是:我将请求的标题设置为“multipart/form data,boundary=%AaB0
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]完成处理程序:^(NSURLResponse*响应,NSData*数据,NSError*错误){
//}] 谢谢你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.
您能否添加部分代码以显示您要发布的内容?当使用Facebook的Graph API库时,图像以JSON编码的数组发送,格式为
basename($file)=>“@”。$file
where$file具有文件位置。在这里,我将代码添加到问题中,我想它会更清楚一点(仅一点…)。我跟随了How to文章,并且能够使用源
作为文件输入框的名称
成功发布该图像,该文件位于我的本地系统中,表单在登录Facebook帐户后将其提交给/me/photos
端点。你能在你的技术堆栈中尝试同样的东西吗?