【问题标题】:Facebook Graph API Upload Local Image to Wall (Objective-C)Facebook Graph API 上传本地图片到墙上(Objective-C)
【发布时间】:2011-11-30 22:19:47
【问题描述】:

我正在尝试将用户创建的本地照片从我的应用程序上传到用户的 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 键修改为imagephotopicturefiledata,但看不到任何变化,而source 似乎是最正确的。

在阅读了关于上传照片的文档:http://developers.facebook.com/docs/reference/api/photo/ 之后,我了解到 source 参数对应一个 URL。我试过直接在用户的电脑上上传图片的数据或者图片的本地路径,但是我还没有找到一种方法来上传一个还没有在互联网上的图片。我确信有办法做到这一点,甚至上传这个词也暗示图像应该是本地的!

基本上,我的问题似乎与此相反:Upload a hosted image with Facebook's Graph API by url?

提前感谢任何可以提供帮助的人,我的 Facebook 朋友开始对所有这些测试消息感到困惑 :)

【问题讨论】:

    标签: objective-c facebook facebook-graph-api


    【解决方案1】:

    您也可以直接上传照片,无需制作网址。 这是示例代码-

    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"];
    

    【讨论】:

      【解决方案2】:

      我刚刚尝试了这里帖子中的方法:Facebook connect on iOS picture doesn't display with wall post,它可能会对您的事业有所帮助。 但是,这种行为似乎与真正的“墙贴图”略有不同,因为图片是上传到您的应用程序相册而不是“墙相册”。

      当您使用 Facebook 应用在几分钟内发布多张图片时,Facebook 会提到“[YourFBName] 已将多张图片添加到 [ApplicationName] 相册”(针对不同事件的“分组”墙贴)。

      而当您直接使用FB界面在墙上发布多张图片时,即使它们都添加到“墙相册”中,它们也始终显示为单独的帖子(此处不分组墙帖)。

      【讨论】:

      • 感谢您的帮助。该代码很有趣,但我已将问题缩小到我用来与 Facebook 交互的库。事实上,PhFacebook 只允许您将图像作为 URL 上传,而不是作为 NSData ......我必须使用这个库,因为它是 Facebook 唯一可以很好地处理登录过程的 Mac API,我不知道如何移植Facebook iOS 的登录对话框。你有什么想法吗?
      【解决方案3】:

      事实证明,您只能将 PhFacebook 用于更简单的请求。以下是我必须在 Mac 上不使用 PhFacebook 将本地图像上传到 Facebook Wall:

      -使用 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 中获得灵感,它使用了类似(但略有不同)的技术。

      【讨论】:

      • +1,它的工作很酷,但我想将图像发布到用户的墙上?
      猜你喜欢
      • 2012-12-05
      • 2013-04-19
      • 2012-08-19
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多