Cant Post To Facebook with Graph API iPhone - iphone

i have implemented this sample code below to get a connection to the user feed post
- (void)viewDidLoad
{
[super viewDidLoad];
facebook = [[Facebook alloc] initWithAppId:#"197765190297119" andDelegate:self];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
facebook.accessToken = [defaults objectForKey:#"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:#"FBExpirationDateKey"];
}
and this sample code is called when the user logs in... it calls now by a button pressed for check, but dosent do any, perhaps i can get a dialog feed insted
NSString *str=#"Your String to post";
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
str,#"message",
#"Test it!",#"name",
nil];
Facebook *fb = [[Facebook alloc] init];
[fb requestWithGraphPath:#"me/feed" // or use page ID instead of 'me'
andParams:params
andHttpMethod:#"POST"
andDelegate:self];

This is a walkthrough of uploading an image to the facebook wall. I copied this out of an older application, the current facebook API works a little bit different. I think you can see my main point of using a shared facebook object, which you use to do the authentication and also the requests to the API. I took out a few things to make it easier to understand. Apple actually wants you to check for an existing internet connection. I hope it helps.
#synthesize facebook;
//login with facebook
- (IBAction) facebookButtonPressed {
if (!facebook || ![facebook isSessionValid]) {
self.facebook = [[[Facebook alloc] init] autorelease];
NSArray *perms = [NSArray arrayWithObjects: #"read_stream", #"create_note", nil];
[facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
}
else {
[self fbDidLogin];
}
}
//upload image once you're logged in
-(void) fbDidLogin {
[self fbUploadImage];
}
- (void) fbUploadImage
{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
resultImage, #"picture",
nil];
[facebook requestWithMethodName: #"photos.upload"
andParams: params
andHttpMethod: #"POST"
andDelegate: self];
self.currentAlertView = [[[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Facebook", #"")
message:NSLocalizedString(#"Uploading to Facebook.", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles: nil] autorelease];
[self.currentAlertView show];
}
//facebook error
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"")
message:NSLocalizedString(#"Facebook error message", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[myAlert show];
[myAlert release];
}
//facebook success
- (void)request:(FBRequest*)request didLoad:(id)result
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Facebook", #"")
message:NSLocalizedString(#"Uploaded to Facebook message", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[myAlert show];
[myAlert release];
}

Related

how to post text to friend wall in Facebook in iphone sdk?

i want to post text to friend wall but there is some problem by using this code...
-(IBAction)PostToBuddyWall
{
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
[postVariablesDictionary setObject:#"LOL" forKey:#"name"];
[postVariablesDictionary setObject:#"helllo" forKey:#"message"];
Facebook *fb = [((AppDelegate*)[[UIApplication sharedApplication] delegate]) fbInstance];
[fb requestWithGraphPath:[NSString stringWithFormat:#"%#/feed",self.fbFriendsInvited] andParams:postVariablesDictionary andHttpMethod:#"POST" andDelegate:nil];
NSLog(#"message: %#",postVariablesDictionary);
[postVariablesDictionary release];
UIAlertView *facebookAlter=[[UIAlertView alloc] initWithTitle:#"Message" message:#"Posted successfully on facebook" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil, nil];
[facebookAlter show];
[facebookAlter release];
[self dismissViewControllerAnimated:YES completion:nil];
}
please give the suggestions if u have...
thanks !!
Try this :
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
[postVariablesDictionary setObject:#"LOL" forKey:#"name"];
[postVariablesDictionary setObject:#"helllo" forKey:#"message"];
//Post to friend's wall.
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/feed", self.fbFriendsInvited] parameters: postVariablesDictionary HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
NSLog(#"%#",result);
}];

Error: HTTP status code: 400 i am getting this while posting data on friend face book wall?

I am selecting the my face book friends in my app i want to post some text data on my friends wall as i pressed the done button when i pressed the done button i am getting this this error.Error: HTTP status code: 400
hehe is my code
- (void)facebookViewControllerDoneWasPressed:(id)sender {
NSMutableString *text = [[NSMutableString alloc] init];
// we pick up the users from the selection, and create a string that we use to update the text view
// at the bottom of the display; note that self.selection is a property inherited from our base class
for (id<FBGraphUser> user in self.friendPickerController.selection) {
NSString *userID = user.id;
NSLog(#"trying to post image of %# wall",userID);
NSMutableDictionary *postVariablesDictionary = [[NSMutableDictionary alloc] init];
//[postVariablesDictionary setObject:UIImagePNGRepresentation(image) forKey:#"picture"];
[postVariablesDictionary setObject:#"my image" forKey:#"message"];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos",userID] parameters:postVariablesDictionary HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Facebook" message:error.localizedDescription delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else
{
//showing an alert for success
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Facebook" message:#"Shared the photo successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}];
}
NSLog(#"%#",text);
[self fillTextBoxAndDismiss];
}

How to post image along with Text using Graph api?

the below code successfuly post my text on facebook wall now i want to post a image along with text using Below code
- (IBAction)callFacebookAPI:(id)sender
{
[self.txtinputfield resignFirstResponder];
if (txtinputfield.text.length !=0)
{
//create the instance of graph api
objFBGraph = [[FbGraph alloc]initWithFbClientID:FbClientID];
//mark some permissions for your access token so that it knows what permissions it has
[objFBGraph authenticateUserWithCallbackObject:self andSelector:#selector(FBGraphResponse) andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins,publish_checkins,email"];
}
else
{
UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"Kindly enter data in the text field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objAlert show];
}
}
- (void)FBGraphResponse
{
#try
{
if (objFBGraph.accessToken)
{
SBJSON *jsonparser = [[SBJSON alloc]init];
FbGraphResponse *fb_graph_response = [objFBGraph doGraphGet:#"me" withGetVars:nil];
NSString *resultString = [NSString stringWithString:fb_graph_response.htmlResponse];
NSDictionary *dict = [jsonparser objectWithString:resultString];
NSLog(#"Dict = %#",dict);
NSMutableDictionary *variable = [[NSMutableDictionary alloc]initWithCapacity:1];
[variable setObject:txtinputfield.text forKey:#"message"];
[objFBGraph doGraphPost:#"me/feed" withPostVars:variable];
UIAlertView *objAlert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"String posted on your wall and you may check the console now" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objAlert show];
}
}
#catch (NSException *exception) {
UIAlertView *objALert = [[UIAlertView alloc]initWithTitle:#"Alert" message:[NSString stringWithFormat:#"Something bad happened due to %#",[exception reason]] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[objALert show];
}
txtinputfield.text = clearText;
}
I tried some methods but did'nt work for me, i have no experience with Graph Api Any help will be appriated.Thanks
Try like below:
- (IBAction)buttonClicked:(id)sender
{
NSArray* permissions = [[NSArray alloc] initWithObjects:
#"publish_stream", nil];
[facebook authorize:permissions delegate:self];
[permissions release];
}
- (void)fbDidLogin
{
NSString *filePath =pathToImage;
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, pathToImage,
#"picture/jpeg", #"contentType",
#"Video Test Title", #"title",
#"Video Test Description", #"description",
nil];
[facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
-(void)fbDidNotLogin:(BOOL)cancelled
{
NSLog(#"did not login");
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
if ([result isKindOfClass:[NSArray class]])
{
result = [result objectAtIndex:0];
}
NSLog(#"Result of API call: %#", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
NSLog(#"Failed with error: %#", [error localizedDescription]);
}

How to post images in facebook in iphone app?

could anyone pls help me to post fotos on facebook using objective c for iphone app.i tried it but its getting terminated when i check with iphone.its working properly in simulator.following is my code i used to post in fb.i use graph api for developing my app.
-(void)fbGraphCallback:(id)sender {
if ((fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0)) {
NSLog(#"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");
//restart the authentication process.....
[fbGraph authenticateUserWithCallbackObject:self
andSelector:#selector(fbGraphCallback:)
andExtendedPermissions:#"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];
[self.view addSubview:viewshare];
}
else {
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:2];
//imgPicture is my image view name
NSLog(#"the Data::%#\n",imgPicture.image);
FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:imgPicture.image];
[variables setObject:graph_file forKey:#"file"];
[variables setObject:[NSString stringWithFormat:#"%#", txtComment.text] forKey:#"message"];
FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:#"me/photos" withPostVars:variables];
NSLog(#"postPictureButtonPressed: %#", fb_graph_response.htmlResponse);
NSLog(#"Now log into Facebook and look at your profile & photo albums...");
txtComment.text=#" ";
[txtComment setHidden:YES];
[lblcmt setHidden:YES];
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:nil message:#"Successfully posted...Now log into Facebook & look at your profile" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
[fbGraph release];
}
First I check if facebook(the facebook object) has a valid session:
if (![facebook_ isSessionValid]) {
permissions_ = [[NSArray arrayWithObjects:#"read_stream", #"publish_stream", #"offline_access",nil] retain];
[facebook_ authorize:permissions_];
}
When I can guaranty that i'm logged into facebook I post the image like this:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, #"picture",
message, #"message",
nil];
[facebook_ requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
Finally I check this methods in for being sure if the image post was succesful or not:
-(void)request:(FBRequest *)request didFailWithError:(NSError *)error {
//Error
}
-(void)request:(FBRequest *)request didLoad:(id)result {
//Succes
}

How to upload photos to Plixi (Tweetphoto) using Oauth?

I am creating an iphone application in which I have to upload photos using different services.
I am successful in uploading photos with Twitpic & YFrog but not able to do with Plixi.
I am using Oauth ,as Twitter is not allowing Basic authentication.
If anyone has tried with Plixi ,please help me out!!
I have googled a lot but not getting any relevant documentation for the new Oauth for Plixi.
Finally, I am with a solution for my problem :)
If anyone is also stuck with this problem, just add the TweetPhoto folder from the following application link:
http://code.google.com/p/tweetphoto-api-objective-c/downloads/detail?name=TPAPI-Objective-C-Library.zip
Change the tweetphoto urls for plixi now.
Also, can refer to my following code for making function calls:
-(void)uploadtoTweetPhoto{
NSString *message = [self.tweetTextView.text stringByReplacingOccurrencesOfString:#"Max. 140 characters" withString:#""];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
if((message == nil) || ([message isEqual:#""])){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Please enter your tweet message.." message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
else{
[dictionary setObject:message forKey:#"message"];
}
if([dictionary count] == 1){
[indicator startAnimating];
[NSThread detachNewThreadSelector:#selector(uploadPhotoToTweetPhoto:) toTarget:self withObject:dictionary];
}
[dictionary release];
}
- (void)uploadPhotoToTweetPhoto:(NSDictionary *)dictionary{
NSString *message = [dictionary objectForKey:#"message"];
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *accessTokenKey = [[NSUserDefaults standardUserDefaults] valueForKey:#"oauth_token"];
NSString *accessTokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:#"oauth_token_secret"];
TweetPhoto *tweetPhoto = [[TweetPhoto alloc] initWithSetup:accessTokenKey identitySecret:accessTokenSecret apiKey:Plixi_API_Key serviceName:#"Twitter" isoAuth:YES];
NSData *dat =[tweetPhoto upload:UIImageJPEGRepresentation(self.imgView.image,0.8) comment:message tags:#"" latitude:23.4646 longitude:-87.7809 returnType:TweetPhotoCommentReturnTypeXML];
NSString *status =[NSString stringWithFormat:#"%i",[tweetPhoto statusCode]];
[tweetPhoto release];
[self performSelectorOnMainThread:#selector(photoUploadedtoTweetPhoto:) withObject:status waitUntilDone:[NSThread isMainThread]];
[pool release];
}
- (void)photoUploadedtoTweetPhoto:(NSString*)status{
[indicator stopAnimating];
if([status isEqualToString:#"201"])
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Tweet Posted" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Tweet Failed" message: #"" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
I'm curious as to the phrase you used in Google. In any case, googling "plixi api" tool me to this page, which links to a Cocoa wrapper on Google Code.