Facebook curl like post request using open graph - iphone

I want to create the like button in my iOS app by using
curl -X POST \
-F 'access_token=USER_ACCESS_TOKEN' \
-F 'object=OG_OBJECT_URL' \
https://graph.facebook.com/[User FB ID]/og
.likes given by Open graph api.
For it I have made post request i.e.
NSString *stringurl=[NSString stringWithFormat:#"{access_token=AAAE08AxGmwghghghOWAxMZBz6jZAaRTUYTRMakJln3VdAeUju6E6tFn2I9feY5wtGatTm2KIgXoXYFZB0pTPQt2Oj0sNaRuX3AZDZD}&{object=http://www.google.com}&https://graph.facebook.com/100002097766610/og.likes"];
NSURL *url = [NSURL URLWithString: stringurl];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
[request1 setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"string..:%#",string);
But I am getting empty string. May be I am making wrong url string. If any one know that how to make the curl post request. Please give me some idea.
Thanks to all.

I attached some codes below:
Body data of Http POST request should located in 'Body', So I used 'setHttpBody:' method.
NSString *userId = #"Your ID Facebook ID";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/%#/og.likes", userId]];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
NSString *body=[NSString stringWithFormat:#"access_token=YOUR_APP_ACCESS_TOKEN&object=OG_OBJECT_URL"];
[request1 setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"string..:%#",string);

Related

Objective-C equivalent of curl request

I'm trying to manipulate this curl request in Objective-C:
curl -u username:password "http://www.example.com/myapi/getdata"
I've implemented the following and I'm getting a data get error Domain=kCFErrorDomainCFNetwork Code=303 with NSErrorFailingURLKey=http://www.example.com/myapi/getdata:
// Make a call to the API to pull out the categories
NSURL *url = [NSURL URLWithString:#"http://www.example.com/myapi/getdata"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
// Create the username:password string for the request
NSString *loginString = [NSString stringWithFormat:#"%#:%#", API_USERNAME, API_PASSWORD];
// Create the authorisation string from the username password string
NSData *postData = [loginString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
I was hoping that someone could spot where I am going wrong with my attempt to manipulate the curl request and point me in the correct direction. Is there something obvious I'm missing out? The data returned from the API is in a JSON format.
I found that the best thing to do was to not attempt the authentication within the code and to put it straight in the URL itself. The working code looks as follows:
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:#"http://%#:%##www.example.com/myapi/getdata", API_USERNAME, API_PASSWORD]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:#"GET"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
This guide seems to do what you're looking for:
http://deusty.blogspot.co.uk/2006/11/sending-http-get-and-post-from-cocoa.html
Just as an FYI, a lot of classes accept initWithData, and NSData has a method dataWithContentsOfURL, if you want to avoid setting up NSURLConnections yourself this could be an easier way of achieving what you're looking for.

How to use .NET web service in iPhone, with POST method?

I am communicating with a .NET web service in an iPhone application, using the POST method, but I am not getting XML back as response. Here is my code:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc"]];
NSString *api = #"/registeruser";
NSData *body = [[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text ,txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str] dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(#"url %#",url);
NSLog(#"body %#",[NSString stringWithFormat:#"%#/%#/%#/%#/%#/%#/%#/%#/%#",
api, txtFName.text, txtLName.text,
txtUName.text, txtEmail.text, txtPassword.text,
txtCity.text, txtState.text, str]);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPBody:body];
[request setValue:#"text/xml" forHTTPHeaderField:#"Content-Type"];
NSLog(#"body len %d",[body length]);
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Please help me. Thanks in advance.
I have done that with following.
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"Myserver/EventService.svc/webservice/%#/%#",para1,para2]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request addValue:0 forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *XMLString=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"data %#",XMLString);
Have you tried something smaller first? Say doing an http post locally? Or doing an http post to that server with a simple html form?
I have run into this issue myself and decided to step back and start with a simple http post to make sure my objective-c was correct first.
I used this tutorial to get me started -hope it helps

Fetch POST value in coldfusion from objective C

I am trying to post a string to a URL in iphone which will be retrieved by the server. The server side scripting is done in coldfusion. I am trying to fetch the data in coldfusion and pass on the value back to iphone. I am not familiar much with coldfusion and wanted some help in this. Below is the coding in iphone :
/** Iphone code **/
NSString *post =[[NSString alloc] initWithString:#"Hello World"];
NSURL *url=[NSURL URLWithString:#"http://www.abc.com/test.cfm"];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
/*Coldfusion code */
<html>
<head><title>Test</title></head>
<body>
<cfoutput>#form.post#</cfoutput>
</body>
</html>
Am I doing it correctly ?
You may need to specify name of form variable in data. Try below for post variable. I haven't tested it should work.
NSString *post =[[NSString alloc] initWithString:#"post=Hello%20World"];

json data reply nil

NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"text/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"text/json" forHTTPHeaderField:#"Accept"];
NSError *error ;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog(#"jsondata=%#,%#",json_string,response);
2011-02-11 12:01:16.653
mystack[1946:207] jsondata=,(null)
Your URL string is not valid since it contains blank spaces: &. You need to escape those before creating an NSURL object. Replace:
NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
with:
NSString *urlString =[NSString stringWithFormat:#"http://api.stackoverflow.com/1.0/questions?tagged=iphone & key=%d",key1];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
thanks for helping me here is the answer
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
the code that i have to change..
You should NSLog your error variable to see what happened: [error localizedDescription]

how to pass arguments to url (.php file)

http://something.php?ownerID=13&view=userAgreement
i need to pass two variables to this ..
how can i pass these i am declared ownerid to 13 in my config playlist and i am able to get it ,how to set "view = useragreement" string to view variable....
can any body show me sample code..
i am using like
NSString* termsURL = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"termsURL"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#%#%#%#%#", termsURL, #"?ownerID=", ownerID,#"?view=",userAgreement]];
but is not working,i am checking parsing is success or not with this url but is failing...
Code taken straight from my app. It works for what you need
NSString *post = [NSString stringWithFormat:#"ownerID=13&view=userAgreement"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
//set up the request to the website
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:#"http://something.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *result3=[[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]autorelease];