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

时间:2011-10-10 13:08:37

标签: iphone objective-c ios-4.2

我已经制作了一个应用程序,在我的应用程序中,我整合了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];
}

但是,我知道了 responseString是 {“error”:{“message”:“验证应用程序时出错。”,“type”:“OAuthException”}} 和  照片ID为:( null)

并且图片不会在用户的墙上上传。 所以,请告诉我如何解决它。

5 个答案:

答案 0 :(得分:1)

首先,您是否授权您的应用程序通过执行此操作上传图片(使用FBConnect SDK),您可以签出所有权限here

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

接下来的问题是facebook不允许以这种方式发送的帖子链接到他们域上托管的图片(我知道它真的很烦人,也许值得检查自4月以来事情是否发生了变化)。我花了很多时间来完成这个。我破解它的方法是使用bitly创建一个重定向URL(你可以使用他们的API以编程方式访问他们的服务,它有一个obj-c包装器here,虽然我将它改为异步)并在帖子中发送该网址。

答案 1 :(得分: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];

答案 2 :(得分: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];
希望,这有助于你...... :)

答案 3 :(得分:0)

facebook API使用OAuth来验证请求。您将需要使用可以请求相应临时/客户端令牌的库,并为请求生成适当的HTTP头。这是一个非常复杂的过程,涉及请求参数的散列和规范化。

您无法制作自己的手动HTTP请求。

答案 4 :(得分: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");
       }

}];

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