How to upload photos to facebook using Graph API in iPhone?

I made one application. In my application, I have integration with Facebook to share information. To integrate with Facebook, I use the Graph API in the application. Now in my application I want to upload a photo to the user's wall. I use this code to upload photos to the user's wall.

// to upload a photo

- (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]; } 

But here I get responseString {"error": {"message": "Application Validation Error.", "Type": "OAuthException"}} and Photo ID: (null)

and the image does not upload to the user's wall. so please tell me how to solve it.

+4
source share
5 answers

can help u

  //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
source

First you authorized your image upload application by doing this (using the FBConnect SDK), you can check all permissions here

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

The next problem is that facebook doesn’t allow messages sent in such a way as to refer to images hosted on their domains (I know this is REALLY annoying, maybe it’s worth checking if the changes have changed since April). I spent a lot of time on this. As I hacked it, it was creating a redirect URL using bitly (you can access their services programmatically using your API, there obj-c wrapper for it here , although I changed it as asynchronous) and send this URL to message.

+1
source

another way below for image on 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]; 

hope this helps you ... :)

+1
source

The facebook API uses OAuth to authenticate requests. You will need to use a library that can request the appropriate temporary / client tokens and create the appropriate HTTP headers for the requests. This is a rather complicated procedure, which includes hashing and normalizing query arguments.

You cannot do this by creating your own manual HTTP requests.

0
source

You can use your own facebook function.

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

Using this code, you can send an image with a message. Best wishes

0
source

All Articles