Posting picture on Facebook 'feed' from an iPhone app - ios

I am using ASIHTTPRequest to work on Facebook graph API.
This is the nearest I have gone to posting a picture on the feed. So if I have a
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
The url is https://graph.facebook.com/me/feed
Now to post a picture I do the following:
[request setPostValue:#"My Message" forKey:#"message"];
[request setPostValue:#"somepic.png" forKey:#"picture"];
[request setPostValue:#"Some Name" forKey:#"name"];
[request setPostValue:#"Some description" forKey:#"description];
[request startAsynchronous];
If you try this then everything works fine other than the picture being posted. A blank placeholder for the picture is though show on the feed.

Just use this small snippet to upload a image on your disk as raw data
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setData:UIImageJPEGRepresentation(img.image, 0.1) withFileName:filename andContentType:#"image/jpeg" forKey:#"photo"];

I found out this documentation on facebook: http://developers.facebook.com/docs/reference/api/post
According to this documentation we can not post an image residing on our machine using the api. Instead we have to upload the pic and then provide the link for posting to the feed.
But uploading an image on to the wall is allowed if you logon to facebook.com. I believe this functionality is not provided in the graph api.

It might be worth taking a look at ShareKit - http://www.getsharekit.com/
Their demo app publishes a photo on the Facebook wall successfully. I believe they wrap the upload-then-publish process in their code.
HTH,
Oded

Related

Authentication Issue with REST call for iOS

I am currently trying to make a REST call from an iOS device. My code is below
NSString *restCallString = [NSString stringWithFormat:#"MyURL"];
NSURL *url = [NSURL URLWithString:restCallString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
[request setHTTPMethod:#"GET"];
[request addValue:Value1 forHTTPHeaderField:#"Header1"];
[request addValue:Value2 forHTTPHeaderField:#"Header2"];
[request setURL:[NSURL URLWithString:restCallString]];
#try{
_currentConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
#catch(NSError *e){
NSLog(#"%#", e.description);
}
Whenever this is called, I get the following error: Authentication credentials were not provided. However, what confuses me is that if I send an identical GET request via a HTTP web console, it works perfectly. In other words, using the same URL and the same 2 header-value pairs, I get a valid response on a web console, and see no authentication errors. What could be causing this?
You are setting the HTTP headers. This won't work, because the HTTP header is not contained in $_GET or $_POST because they're are not content, but description of the content expected.
Try this instead:
NSURL *url = [NSURL URLWithString:[restCallString stringByAppendingFormat:#"?Header1=%#&Header2=%#", Value1, Value2]];
Of cause you have to be aware that the URL is RFC 1738 compliant.
if I send an identical GET request via a HTTP web console, it works perfectly
I suspect your web console is leveraging SessionAuthentication — i.e. If you're already logged in to your site in your browser the API will authenticate you based on your session cookie.
Django Rest Framework provides various authentication methods and there are third-party options too. The simplest to get going is probably the provided Token Auth method.
Make sure this is enabled. Create a token in the admin (or via the provided view) and make sure you've set the Authorization header. It needs to look like this:
Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b
So your Objective-C will go something like:
[request addValue:[NSString stringWithFormat:#"Token %#", yourToken]
forHTTPHeaderField:#"Authorization"];
Hopefully that gets you started.

ASIHTTPRequest addRequestHeader issue

I am using ASIHttpRequest and make use GET method to send header to server. I call method addRequestHeader like this
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:#"myHeader" value:#"abc"];
It's not working. But if I use NSMutableURLRequest to add header and request to server, it works.
I don't know anything wrong when calling addRequestHeader methods for ASIHTTPRequest library.
Have anyone seen this issue?
Wow ok so, yeah if this is a new app, please do NOT use ASIHttpRequest. It has long been supplanted by the delightful AFNetworking.
If this is an existing application, you really should work on a migration plan off ASI.
However, in an attempt to actually answer your question - that is the appropriate setup per the documentation, and is how I used to use it. My guess is something is broken under the covers and judging from a basic google request, there are issues with iOS 7 including memory leaks and requests just failing.
You can do it via NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.abc.com"]];
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:#"AAA" forHTTPHeaderField:#"Hello-there"];
request = [mutableRequest copy];
NSLog(#"%#", request.allHTTPHeaderFields);
Hope this helps .. :)

Disqus Password Credentials OAuth

I'm trying to authenticate a user in my iOS app but all I get is a 400 error.
According to the documentation, "this type of flow is restricted to approved applications only, so you must request access first".
So how do I approve my application to be able to accomplish this flow?
Part of my request:
NSURL *url = [NSURL URLWithString:#"https://disqus.com/api/oauth/2.0/access_token/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:WS_TIMEOUT];
NSString *strAuth = [NSString stringWithFormat:#"%#:%#", username, password];
NSString *strAuthBase64 = [[strAuth dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
NSString *postString = [NSString stringWithFormat#"grant_type=password&client_secret=%#&client_id=%#&scope=read,write", DISQUS_SECRET, DISQUS_KEY];
[request addValue:[NSString stringWithFormat:#"Basic %#", strAuthBase64] forHTTPHeaderField:#"Authorization"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
Thanks in advance.
This is something we'd have to enable for your application from our end. However, I'd instead recommend hosting a page to handle the authentication with the standard server-side flow. You can then pull the access token and other variables from the page into your application after the user has authorized.
The reason is so you don't have to deal with form validation, error messaging, and can take advantage of our updates to the form without touching your code.
Some server-side OAuth examples in PHP and Python can be found on this page: https://github.com/disqus/DISQUS-API-Recipes/tree/master/oauth
Try this library which solves Disqus authorization issue in a slick manner. Really nice solution https://github.com/moqod/disqus-ios

Unable to Like a post from ios sdk

i am using Facebook SDK 3.0 to login user and i am successfully getting its FB id and FB session token.
`NSDictionary<FBGraphUser> *user.id` (FB ID)
and
[FBSession.activeSession accessToken] (FB session token)
Now i want to like a post inside my app. SO i am creating a POST request and passing the parameters like this
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/og.likes",#"34856345..."]] cachePolicy:NO timeoutInterval:5.0];
[urlRequest addValue:#"BAAGjjgD8bhUBAM73jzkpy23zlHo7e8ZAqPaOGvbxU..." forHTTPHeaderField:#"access_token"];
[urlRequest addValue:myshareLink forHTTPHeaderField:#"object"];
[urlRequest setHTTPMethod:#"POST"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self startImmediately:YES];
[theConnection autorelease];
but i am getting this error
result:{"error":{"message":"An access token is required to request this resource.","type":"OAuthException","code":104}}
but if i use the same access token and user id from CURL command it posts successfully.
curl -X POST -F 'access_token=BAAGjjgD8bhUBAE8XmogjJZAZAPyJ8SUyxkllJQ...' -F 'object=http://xyz.com/abc/gallery.php#pic/pic2/' https://graph.facebook.com/732../og.likes
Please some help me figure it out what i am missing.
Thanks
I would recommend using the FBRequestConnection and FBRequest class found in the iOS Facebook SDK rather than using NSURLConnection and NSMutableURLRequest. I believe Facebook requires some extra header values at the iOS level that the Facebook SDK automatically generates for you.
For a like action, I used the FBRequest class method requestForGraphPath to create the request. Don't forget to set the httpMethod and session fields on your FBRequest before passing it to a FBRequestConnection.

ASIHTTPRequest Clearing Cache for only one URL

I'm using ASIHTTPRequest with caching for downloading pictures in my iOS app. Most pictures never change and hence can leverage the caching functionality. There are a few that do change based on a certain function in the iOS app. So, I know exactly when these images will change. How can I clear the cache of a certain link (e.g. http://www.test.com/image.jpg) and preserve the cache for all other requests in my app. Thanks.
ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:#"{Image url}"]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedWhenStaleCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setSecondsToCache:60*60*24*7];
[[ASIDownloadCache sharedCache] removeCachedDataForURL:[NSURL URLWithString:#"{Image url}"]];

Resources