如何在iPhone上使用Graph API在Facebook上上传照片?
我已经做了一个应用程序,在我的应用程序中我已经整合了Facebook来共享信息。用于Facebook集成的 我在应用程序中使用Graph API。 现在在我的应用程序中,我想在用户的墙上上传照片。 我已经使用此代码在用户的墙上上传照片。如何在iPhone上使用Graph API在Facebook上上传照片?
//上传照片
- (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是{ “错误”:{ “消息”: “错误校验应用程序”, “类型”: “OAuthException”} }和 照片ID是:(null)
图像不会在用户的墙上上传。 所以,请告诉我如何解决它。
首先有您授权您的应用程序通过这样(使用FBConnect SDK)上传的图片,您可以检出的所有权限here
NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil];
[facebook authorize:permissions];
下一个问题是,Facebook的不允许以这种方式发送的帖子链接到他们的域名托管的图片(我知道这真的很烦人,也许值得检查,如果事情发生了变化,自4月以来)。我花了相当多的时间来完成这一个。我破解它的方式是使用bitly创建重定向URL(您可以使用它们的API以编程方式访问它们的服务,但它有一个obj-c包装器,但我将其更改为异步),并在该帖子中发送该URL。
可以帮助ü
//facebook post a image on wall
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
userImgView.image, @"picture",
nil];
[facebook requestWithGraphPath:@"me/photos"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
facebook是什么样的数据类型 – 2014-03-20 13:54:45
另一种方式是波纹管在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];
希望,这帮助你... :)
Facebook的API使用OAuth对请求进行身份验证。您将需要使用可以请求适当的临时/客户端令牌的库,并为请求生成适当的HTTP头。这是一个非常复杂的过程,涉及请求参数的哈希和规范化。
你不能这样制作你自己的手动HTTP请求。
您可以使用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");
}
}];
使用此代码,你可以上传图片与消息。 问候函
正如我在我的回答中所说的,您必须授权facebook才能获得有效的访问令牌。你在使用iOS的Facebook SDK吗? http://developers.facebook.com/docs/guides/mobile/ios/ – jbat100
我也有同样的问题你解决了吗?如果可以,请告诉造成此错误的原因。 – Vijay