views:

19

answers:

1

Can someone please explain to me how to create an ASIHTTPRequest object that can post a stream to facebook using the Graph API?

The API call is: https://graph.facebook.com/me/feed?message=X&access_token=Y The method type is POST

A: 

I've never posted directly to the Facebook Graph api (though I have used their API that pulls up UIWebView dialogs and handles the actual posting business for you). I've used the heck out of ASIHTTPRequest, though.

NSString *reqStr = [NSString stringWithFormat:@"https://graph.facebook.com/me/feed?message=%@&access_token=%@", messageString, accessTokenString];
ASIHTTPRequest *req = [ASIHTTPRequest requestWithUrl:[NSURL urlWithString:reqStr]];
req.delegate = self;
req.userInfo = [NSDictionary dictionaryWithObject:@"graphRequest" forKey:@"type"];
req.requestMethod = @"POST";
[req startAsynchronous];

Couple things. I demonstrate the usage of the .userInfo field because it's just so darn useful. That property takes any NSDictionary containing any objects you want to include, and you can get to them later in the -request: didFinish method. You could pass it a pointer to the UIImageView image you want to load with the UIImage you're downloading, for instance. More frequently, in my experience, if you have more than one request happening inside the same viewController, you just set a different object for your @"type" key in that dictionary, and you've got an easy way to tell them apart later.

Second, you might well ask, "Do I have to do anything special because it's https?" In my experience, you totally don't, ASIHTTPRequest is smart enough to handle all that behind the scenes.

If you need to build up post fields, rather than just sending data on the query string, you use an ASIFormPostData object rather than ASIHTTPRequest, but it behaves more or less the same.

Dan Ray