![](/img/trans.png)
[英]Facebook Graph API not posting wall message with Objective-C HTTP post using JSON
[英]Facebook Graph API Upload Local Image to Wall (Objective-C)
我試圖將用戶創建的本地照片從我的應用程序上傳到用戶的Facebook牆。 我正在Objective-C中開發一個應用程序,並使用PhFacebook庫與社交網絡進行交互,但我認為我的問題更有可能是由於我對Facebook實際交互的錯誤使用而引起的。
確實,在獲得訪問令牌后,要將狀態上傳到用戶的個人資料,此代碼可以很好地工作:
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:1];
[variables setObject:@"This is a test." forKey:@"message"];
[fb sendRequest:@"me/feed" params:variables usePostRequest:YES];
但是,如果我嘗試使用以下代碼將照片直接上傳到me/feed
,則會系統地失敗,因為未顯示圖片,並且周圍的文字是:
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];
我嘗試將source
密鑰修改為image
, photo
, picture
, file
和data
,但是看不到任何更改,並且source
似乎是最正確的。
閱讀有關上傳照片的文檔后: http : //developers.facebook.com/docs/reference/api/photo/ ,我了解到source參數對應於一個URL。 我已經嘗試過直接在用戶計算機上上傳圖像的數據或圖像的本地路徑,但是我仍然沒有找到上傳不在Internet上的圖像的方法。 我敢肯定有一種方法可以做到這一點,即使上載一詞也意味着該圖像應該是本地的!
本質上,似乎我遇到了與該問題相反的問題: 通過url通過Facebook的Graph API上傳托管圖像嗎? 。
在此先感謝所有可以提供幫助的人,我的Facebook朋友開始對所有這些測試消息感到困惑:)
您也可以直接上傳照片,而無需創建網址。 這是示例代碼-
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"];
我只是在這里的帖子上嘗試過該方法: iOS上的Facebook連接圖片上沒有顯示牆上的帖子 ,這可能有助於您的原因。 但是,由於圖片是上傳到應用程序相冊而不是“牆相冊”的,因此該行為似乎與實際的“牆相冊”略有不同。
當您使用Facebook應用在短時間內發布多張圖片時,Facebook提到“ [YourFBName]已將幾張圖片添加到[ApplicationName]相冊中”(針對不同事件的“分組”牆貼)。
而當您直接使用FB接口將幾張圖片發布到牆上時,即使它們都已添加到“牆上相冊”中,它們也始終顯示為單獨的帖子(牆上的帖子不在此處分組)。
事實證明,您只能將PhFacebook用於更簡單的請求。 在不使用PhFacebook的情況下,在Mac上,我必須將本地圖像上傳到Facebook牆:
-使用WebView和ASIHTTPRequest(http://www.capturetheconversation.com/technology/iphone-facebook-oauth2-graph-api)獲取具有publish_stream
權限集的Facebook訪問令牌。
-通過簡單的ASIHTTPRequest實際上傳圖片:
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中簡單地啟發自己,該鏈接使用類似(但略有不同)的技術。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.