0

ここでの最初の質問...HTTPPOSTリクエストを使用して、Mac OSアプリ(cocoa)からFacebookに写真をアップロードしようと必死になっています。「https: //graph.facebook.com/me/photos?access_token=.....」に続いて「source=MyURLOnline.jpg」を使用すると、うまく機能しますが、リンクではなくデータをアップロードする必要がありますすでにWeb上にある画像の...

そこで、「フィード」を「写真」に切り替え、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をPOSTする必要があることをご存知の場合は、その方法を理解するために1日を費やしましたが、まだ成功していません。これが私のコードです。「source=URLOnline.jpg」を「feed」にPOSTすると(そして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:request queue:[NSOperationQueue mainQueue] completeHandler:^(NSURLResponse * response、NSData * data、NSError * error){

//}];

4

1 に答える 1

1

Anveshに感謝します。あなたの助けとHTTPPOSTに関するCocoaの投稿により、私はついにNSURLRequestを作成してNSImageをFacebookにアップロードすることができました。SDKがないので、Macアプリを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.
于 2013-03-05T18:44:44.860 に答える