2013-02-25 2 views
0

여기에서 첫 번째 질문은 ... HTTP POST를 사용하여 Mac OS App (cocoa)에서 Facebook으로 사진을 필사적으로 업로드하려고합니다. 의뢰. "https://graph.facebook.com/me/photos?access_token= ....."다음에 "source = MyURLOnline.jpg"을 사용하면 훌륭하게 작동하지만 웹에 이미있는 이미지의 링크가 아니라 DATA를 업로드해야합니다.NSImage를 Facebook-Graph Api (HTTP POST를 통해)에 업로드하는 방법

그래서 "피드"를 "사진"으로 바꾸고 URL을 내 NSImage의 RAW 데이터로 변환합니다 (그리고 어쩌면 "소스 ="에서 "이미지 ="로?). 하지만 요청한 헤더를 "multipart/form-data, boundary = % AaB03x"로 설정하고 "\ r \ n"과 "Content-Type : image/jpeg \ r \ n \ r \ n" "# 324) 파일 업로드 필요"오류가 발생했습니다. ...

저는 몇 년 동안 Cocoa에 대한 경험이 있지만 HTTP 요청에 대해서는 아무것도 모르고 있습니다. 특히 그래프 API가 기대하는 것은 무엇인가, 그래서 내가 발견 한 페이스 북의 모든 도움을 읽었고 몇 가지 실수를 저지르기 때문에 예제를 찾는 것을 좋아했을 것이다. 그러나 그것이 가능한지 전혀 궁금하다.

감사합니다.

업데이트 : 감사합니다. Anvesh. JSON을 POST해야한다는 것을 알고 있다면 어떻게해야 할 지 알아 내려고 노력했지만 성공하지 못했습니다. "피드"에 "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 : 요구 큐 [NSOperationQueue mainQueue] completionHandler^(NSURLResponse * 응답을 NSData * 데이터 오류 NSError *) {

//}];

+0

게시하려는 항목을 표시하기 위해 코드의 일부를 추가 할 수 있습니까? Facebook의 Graph API 라이브러리를 사용할 때 이미지는 JSON으로 인코딩 된 배열로'basename ($ file) => '@'. $ file' 파일로 보내집니다. $ file 여기서 $ file은 저장할 위치가 있습니다. –

+0

여기에 질문에 대한 코드가 추가되어 조금 더 명확해질 것입니다. (조금만 ...) – Raphael

+0

How-to 문서 [https : //developers.facebook. co.kr/blog/post/498 /), ** 파일 입력 상자 **의'이름'으로'source'를 사용하여 성공적으로 이미지를 게시 할 수 있었고 파일은 로컬 시스템에 있었고 양식은 그것을 제출했습니다 페이스 북 계정에 로그인 한 후'/ me/photos' 엔드 포인트로 이동하십시오. 기술 스택에서 똑같은 것을 시도해 볼 수 있습니까? –

답변

1

Anvesh, 도와 주셔서 감사합니다. HTTP POST에 대한 코코아 게시물을 통해 마침내 Facebook에 내 NSImage를 업로드하기 위해 NSURLRequest를 만들었습니다. SDK가 없어 Mac App을 Facebook에 연결하는 데 많은 어려움을 덜어주기를 바랍니다.

// 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. 
관련 문제