2011-11-30 16 views
0

ローカルのユーザー作成の写真を自分のアプリケーションからユーザーのFacebook Wallにアップロードしようとしています。私はObjective-Cでアプリを開発しており、PhFacebookライブラリを使ってソーシャルネットワークとやり取りしていますが、私の問題はFacebookとの実際のやりとりの間違った使用から来ている可能性が高いと思います。実際Facebook Graph API壁紙にローカル画像をアップロードする(Objective-C)

、アクセストークンを得た後、このコードは完璧にうまく機能し、ユーザーのプロファイルにステータスをアップロードする:

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:1]; 

[variables setObject:@"This is a test." forKey:@"message"]; 

[fb sendRequest:@"me/feed" params:variables usePostRequest:YES]; 

しかし、私が使用して、me/feedに直接写真をアップロードしようとすると、私はimageからsourceキーを変更しようとした

NSString *path = @"some/path/to/some/image/on/my/computer"; 

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2]; 

[variables setObject:@"This is a test." forKey:@"message"]; 

[variables setObject:path forKey:@"source"]; 

[fb sendRequest:@"me/feed" params:variables usePostRequest:YES]; 

photo、:画像が示されており、周囲のテキストがあるされていないとして、次のコードは、それが、体系的に失敗します,file、およびdataですが、変更は見られず、sourceが最も正しいようです。

写真のアップロードに関するドキュメント:http://developers.facebook.com/docs/reference/api/photo/を読んだ後、私はソースパラメータがURLに対応していることを理解しました。画像のデータや画像のローカルパスをユーザーのコンピュータに直接アップロードしようとしましたが、まだインターネット上にない画像をアップロードする方法が見つかっていません。私はこれを行う方法があると確信しています。たとえアップロードという単語であっても、画像はローカルでなければなりません!

本質的に、私はこの問題の逆を持っているようです:Upload a hosted image with Facebook's Graph API by url?。助けることができる誰にも事前に

おかげで、私のFacebookの友人は

答えて

2

簡単なリクエストでは、PhFacebookしか使用できません。ここでは、PhFacebookを使わずにFacebook壁面にローカル画像をアップロードする必要があります。

-WebViewとASIHTTPRequest(http://)を使用してpublish_streamアクセス許可を設定したFacebookアクセストークンを取得するwww.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api)。実際に画像をアップロードするための簡単なASIHTTPRequest -make

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me/photos?access_token=%@", access_token]]; 

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    [request addFile:@"/path/tolocalimage/" forKey:@"file"]; 

    [request setPostValue:[NSString stringWithFormat:@"This is a test."] forKey:@"message"]; 

    [request startAsynchronous]; 

そして、そこに行きます!このソリューションは、既存のフレームワークを使用するよりも簡単です!唯一の時間を要するのはWebViewインターフェイスを構築することですが、私が投稿したリンクやPhFacebookから簡単にインスピレーションを得ることができます。hは、(まだわずかに異なる)同様の技術を使用しています。

+0

この回答はどうしてうまくいくのですか? – chunkyguy

+0

+1、それはうまくいっていますが、ユーザーの壁に画像を投稿したいですか? – Dinesh

2

は、私はちょうどここにポストに方法を試してみました:)これらすべてのテスト・メッセージの上に困惑され始めている:Facebook connect on iOS picture doesn't display with wall post、それはあなたの原因を助けるかもしれません。 しかし、壁のアルバムではなくアプリケーションのフォトアルバムに画像がアップロードされるため、動作が実際の「壁の画像の投稿」と少し違うようです。

Facebookアプリを使用して数分間の写真を投稿すると、「[YourFBName]が[ApplicationName]アルバムに複数の写真を追加しました」(別のイベントの「グループ化」壁紙)

FBインターフェースを使用して壁に複数の写真を直接投稿すると、「壁のアルバム」にすべて追加されていても、常に別の投稿として表示されます(ここで壁の投稿はグループ化されません)。

+0

ありがとうございました。そのコードは興味深いですが、Facebookとのやりとりに使用しているライブラリの問題を絞りました。確かに、PhFacebookはNSDataとしてではなく、URLとして画像をアップロードすることができます... Facebook用の唯一のMac APIであるログイン処理をうまく処理するので、このライブラリを使用する必要があります。 FacebookのiOSのログインダイアログ。アイデアはありますか? – elliottbolzan

3

また、URLを作成する必要がないため、写真を直接アップロードすることもできます。ここでは

NSString *urlString = [NSString stringWithFormat:@"https://graph.facebook.com/%@", @"facebook id"]; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSString *boundary = @"----1010101010"; 
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
NSMutableData *body = [NSMutableData data]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] 
     dataUsingEncoding:NSUTF8StringEncoding]]; 
NSEnumerator *enumerator = [post_vars keyEnumerator]; 
NSString *key; 
NSString *value; 
NSString *content_disposition; 
while ((key = (NSString *)[enumerator nextObject])) { 

    //if it's a picture (file)...we have to append the binary data 
    if ([key isEqualToString:@"file"]) { 
     NSData *picture_data = UIImagePNGRepresentation([post_vars objectForKey:key]); 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\";\r\nfilename=\"media.png\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:picture_data]; 
    } else { 
     value = (NSString *)[post_vars objectForKey:key]; 

     content_disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key]; 
     [body appendData:[content_disposition dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[value dataUsingEncoding:NSUTF8StringEncoding]]; 

    } 
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
} 
[body appendData:[@"Content-Disposition: form-data; name=\"access_token\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];  
[body appendData:access_token] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setHTTPBody:body]; 
[request addValue:[NSString stringWithFormat:@"%d", body.length] forHTTPHeaderField: @"Content-Length"];  
NSData *data_reply; 
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:delegate startImmediately:YES]; 
[conn start]; 
BOOL flag = YES; 
NSString *stringResponse = [[NSString alloc] initWithData:data_reply encoding:NSUTF8StringEncoding]; 
NSError *err = [[NSError alloc] init]; 
if (err != nil) { 
    NSData *dataStr = [stringResponse dataUsingEncoding:NSUTF8StringEncoding]; 
    NSMutableDictionary *data = [NSJSONSerialization JSONObjectWithData:dataStr options:kNilOptions error:&err]; 
    if([data objectForKey:@"Error"]!=nil){ 
    } 
}iscr 
stringResponse = nil; 
data_reply = nil; 

コード - サンプル}このpost_varsで

であり、以下のように記述された辞書 -

NSMutableDictionary * post_vars = [NSMutableDictionary dictionaryWithCapacity:4]。

[post_vars setObject:@"this is a message" forKey:@"message"]; 
[post_vars setObject:@"name" forKey:@"name"]; 
[post_vars setObject:@"description" forKey:@"description"]; 
[post_vars setObject:imagename forKey:@"file"]; 
関連する問題