2013-02-25 44 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. 
相關問題