如何在iPhone上使用Graph API上传照片至Facebook?

4
我已经制作了一个应用程序,在我的应用程序中,我集成了Facebook以共享信息。 为了整合Facebook,我在应用程序中使用了Graph API。 现在,在我的应用程序中,我想要上传照片到用户的墙上。 我已经使用以下代码上传照片到用户的墙上。 // 上传照片的代码
- (void)uploadPhoto:(id)sender {


NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Image1" ofType:@"jpg"];

NSString *message = [NSString stringWithFormat:@"I think this is a Great Image!"];

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addFile:filePath forKey:@"file"];
[request setPostValue:message forKey:@"message"];
[request setPostValue:_accessToken forKey:@"access_token"];
[request setDidFinishSelector:@selector(sendToPhotosFinished:)];
[request setDelegate:self];

[request startAsynchronous];
}
- (void)sendToPhotosFinished:(ASIHTTPRequest *)request {

// Use when fetching text data
NSString *responseString = [request responseString];

NSMutableDictionary *responseJSON = [responseString JSONValue];
NSString *photoId = [responseJSON objectForKey:@"id"];
NSLog(@"Photo id is: %@", photoId);

NSString *urlString = [NSString stringWithFormat:
                       @"https://graph.facebook.com/%@?access_token=%@", photoId, 
                       [_accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *newRequest = [ASIHTTPRequest requestWithURL:url];
[newRequest setDidFinishSelector:@selector(getFacebookPhotoFinished:)];

[newRequest setDelegate:self];
[newRequest startAsynchronous];
}

但是,这里我得到的响应字符串是{"error":{"message":"应用程序验证错误。","type":"OAuthException"}}照片id为:(空)

并且图像未上传到用户的墙上。 因此,请告诉我如何解决它。


就像我在我的回答中所说的那样,您必须授权Facebook才能获得有效的访问令牌。您正在使用iOS Facebook SDK吗?http://developers.facebook.com/docs/guides/mobile/ios/ - jbat100
我也遇到了同样的问题,你解决了吗?如果是的话,能否告诉我这个错误的原因。 - Vijay
5个回答

1

首先,您是否已经通过使用FBConnect SDK授权您的应用程序上传图片?您可以在这里查看所有权限

NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil];
[facebook authorize:permissions];

下一个问题是Facebook不允许以这种方式发送的帖子链接到托管在其域名上的图片(我知道这真的很烦人,也许值得检查自四月份以来是否有所改变)。 我花了相当多的时间解决这个问题。 我破解它的方法是使用bitly创建重定向URL(您可以使用他们的API以编程方式访问其服务,这里有一个Objective-C包装器here,虽然我将其改为异步),然后在帖子中发送该URL。

1

可能对您有所帮助

 //facebook post a image on wall
     NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                       userImgView.image, @"picture",
                                       nil];

[facebook requestWithGraphPath:@"me/photos"
                     andParams:params
                 andHttpMethod:@"POST"
                   andDelegate:self];

1
Facebook是什么数据类型? - user2580666

1

在 Facebook 上发布图片的另一种方法如下...

NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];

    //create a UIImage (you could use the picture album or camera too)

    UIImage *picture = [UIImage imageNamed:"yourImageName"];
    //create a FbGraphFile object insance and set the picture we wish to publish on it
    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:picture];

    //finally, set the FbGraphFileobject onto our variables dictionary....
    [variables setObject:graph_file forKey:@"file"];

    [variables setObject:@"write your Message Here" forKey:@"message"];

    //the fbGraph object is smart enough to recognize the binary image data inside the FbGraphFile
    //object and treat that is such.....
    //FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"117795728310/photos" withPostVars:variables];
    FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/photos" withPostVars:variables];

希望这能帮到你... :)

0

你可以使用 Facebook 的原生函数

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: UIImageJPEGRepresentation(postImage, 1.0), @"source", self.postTextView.text, @"message", nil];
            /* make the API call */

[FBRequestConnection startWithGraphPath:@"/me/photos" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
{

       if (error == nil) {
               NSLog("Success");
       }
       else {
               NSLog("Failed");
       }

}];

使用此代码,您可以发布带有消息的图片。 最好的问候。

0
Facebook API使用OAuth来验证请求。您需要使用一个库来请求适当的临时/客户端令牌,并为请求生成适当的HTTP头。这是一个相当复杂的过程,涉及请求参数的哈希和归一化。
您不能通过手动编写HTTP请求来完成此操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接