I have an iOS application which connects via OAuth 2.0 to Facebook. I would like to make a POST request which achieves the equivalent of this code in iOS:
curl -F 'access_token=...' \
-F 'message=Hello. I like this new API.' \
https://graph.facebook.com/[USER_ID]/feed
I found a nice tutorial online and I followed all the instructions, but I still can get my POST request to work. Here is my code:
NSString *post = post_text.text;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *ready_post = [NSString stringWithFormat:#"https://graph.facebook.com/1313573269/feed?message=%#&access_token=CAACEdEose0cBAHgdZAon2EZBZCzjoLkhg7jrqvZAbliuoOQ2E2Exc4rZCAxPzeVEADwnQaNLYuG16Gq6q6LLLLLLLLVQ7LZAncXCc53qE2iyzleZAPXGajsgjnBTuo6YdJCZAxVGIYYD8sZCgQ9ypZCo0iOZBNuyrechBfee2yptlK9tmgdosZA7wrKg8ZD", postData];
[request setURL:[NSURL URLWithString:ready_post]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
As you can see I am hard coding the access token for the time being just as a test. But I cant seem to get my app to actually POST anything to Facebook.
Thanks, Dan.
You're trying to post a JSON to the GraphAPI - which you can't do.
Instead of that, you have to use parameters when posting (query string or HTTP request parameters).
See: Publish to Feed.
Related
I am trying to fetch the number of times a link is shared on google plus in my iOS app. I am trying the following code
NSString *post = #"[{\"method\":\"pos.plusones.get\",\"id\":\"p\",\"params\":{\"nolog\":true,\"id\":\"http://stylehatch.co/\",\"source\":\"widget\",\"userId\":\"#viewer\",\"groupId\":\"#self\"},\"jsonrpc\":\"2.0\",\"key\":\"p\",\"apiVersion\":\"v1\"}]";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat : #"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://clients6.google.com/rpc?key=my_api_key"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSError *err;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:&err];
return data;
but it returns the following error
"Access Not Configured. The API (+1 API) is not enabled for your
project. Please use the Google Developers Console to update your
configuration."
In Google Developers Console
https://console.developers.google.com/project/googeplussignin/apiui/apis/library
Select your project
2.Left Pane : Select Apis & Auth
There you can find so many API. Enable needed API.
It seems google no more allows the use of this api for public use. I found the answer here.
We now need to send a Get request to https://plusone.google.com/_/+1/fastbutton?url=http://www.facebook.com
I've seen many post on this topic, but none of them helped in resolving the problem. I am having a download.php file on webserver which I use for downloading images stored on the server. The php does this by splitting the task into two step.
The first step is I send the user info and the php returns the ids of images associated with the user. In the second step on basis of those ids I send another request to the same php page and request image of a particular id one by one.
This approach works well when used in html or in my android code, so I am sure that there's no issue in the php.
When I do send request to php from ios code, the first step of getting image ids works well, but requesting image doesn't work at all. The didReceieveData callback never gets called. Here's what I am doing -
- (void)downloadImages:(NSInteger)imageNo{
NSString *post = [NSString stringWithFormat:#"user=%#&download=%#&imageId=%d", userName, #"true",imageNo];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
initWithURL:[NSURL
URLWithString:#"MYWEBSERVER/download.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
currentDownMode = DOWN_IMAGE;
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Here's what I am having in my php page -
function download()
{
global $folder_path, $user_id, $image_table, $sql_folder_path;
$fileID = $_POST["imageId"];
$sql="SELECT fileName,filePath FROM $image_table WHERE fileId='$fileID'";
$query = mysql_query($sql) or die($sql . ' - ' . mysql_error());
$row = mysql_fetch_array($query);
$im = file_get_contents("../" . $row[1] . $row[0]);
header('content-type: image/gif');
echo $im;
}
I'm trying to comment on a youtube video via the youtube api. I have to send some XML to their server, but when i do it gives me nothing back, neither comments it on the video.
Heres the link to the api documentation!
POST /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<content>This is a crazy video.</content>
</entry>
I really appreciate all your help, because I'm stuck on this for days.
Thanks!
Here is my code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *requestString = [NSString stringWithFormat:#"<?xml version=\"1.0\" encoding=\"UTF-8\"?><entry xmlns=\"http://www.w3.org/2005/Atom\"xmlns:yt=\"http://gdata.youtube.com/schemas/2007\"><content>%#</content></entry>", [textField text]];
NSData *postData = [requestString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://gdata.youtube.com/feeds/api/videos/4NE7Nmmt0R4/comments"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/atom+xml" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"key=%#", [defaults objectForKey:#"accessToken"]] forHTTPHeaderField:#"Authorization"];
[request setValue:#"key=AI39si4apF3QyQkXbH_C5IHIClkyP2mio2QJ3JBUUpvPbO2rhch7tpYjMavZgt5QzGaGrHBfom5mNpoUq_ZLRPPa35KO21O9Pw" forHTTPHeaderField:#"X-GData-Key"];
[request setValue:#"2" forHTTPHeaderField:#"GData-Version"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
[conn start];
The YouTube API will always send an HTTP response whenever you make an HTTP request. Even if you make a completely invalid HTTP request, you'll get back an HTTP error response. Please double-check to see if you're getting any response back at all. If you're really not, then my guess is that you're not actually completing your HTTP request—perhaps it's never making it to the API servers due to some network problems, or perhaps you're not calculating CONTENT_LENGTH correctly (assuming you're attempting to calculate it by hand).
Also, I'd recommend using the GData Objective-C client library if you're not already.
Hi I am trying to connect to some server which will use username and password as credentials..Following is the code I am using.
NSString post =[[NSString alloc] initWithFormat:#"userName=*&password=*"];
NSURL url=[NSURL URLWithString:#"******"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding 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];
NSLog(#"request:%#",request);
urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
Here the problem is server is giving user invalid message.But the credentials which i am giving is fine.This username and password is not going in request object..can any one of you please help me..
You need to start with a string that holds your username & password. This will form the http POST body. You need to replace username_input_field & password_input_field with the correct input field names for the webserver. My advice is to use Firefox along with the httpFox extension to login to your webserver. httpFox will show you all the input field names for the request. There may also be extra hidden fields which you'll need to add too.
NSString *post = [NSString stringWithFormat:
#"username_input_field=%#&password_input_field=%#",username,password];
Add the username to the request header object, like so:
[request addValue:#"<username>" forKey:#"<username parameter>"]
I've successfully implemented push notifications with Urban Airship in my app, but I want to make an administrative password-protected part of the app that enables one to send the push notifications from any authorized iPhone with the app installed.
I need a way so whatever i type in a text box is send via https POST to https://go.urbanairship.com/api/push/broadcast.
Can anyone recommend or tell me a way to do this?
You can use NSURLConnection to do this. Setup an NSMutableURLRequest with the post data and other information, and then use NSURLConnection to post it and get a response:
NSString * postString = [NSString stringWithFormat:#"field1=%#", myItem];
NSData * postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSURL * myURL = [NSURL URLWithString:#"https://go.urbanairship.com/api/push/broadcast"];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [postData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:postData];
NSData * returnedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
You can also use NSURLConnection for asynchronous requests. See the NSURLConnection Class Reference.