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.
Related
I'm trying to get a response data from dropbox oauth as described here
Now i dont know why it keeps saying {"error": "Invalid OAuth request."}
Here is a my complete code for requesting token:
-(void)requestTokenSecret
{
NSString* oauthVersion=#"1";
NSString* oauth_signature_method=#"PLAINTEXT";
NSString* postHeader = [NSString stringWithFormat: #"Authorization: OAuth oauth_version=\"%#\", oauth_signature_method=\"%#\", oauth_consumer_key=\"%#\", oauth_signature=\"%#&\"",oauthVersion,oauth_signature_method,APP_KEY,APP_SECRET];
NSLog(#"URL HEADER: %#",postHeader);
NSData* postHeaderData = [postHeader dataUsingEncoding:NSUTF8StringEncoding];
NSMutableURLRequest* request= [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:#"https://api.dropbox.com/1/oauth/request_token"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d",[postHeaderData length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postHeaderData];
NSURLConnection* conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
if(conn)
{
NSLog(#"Connected");
}
else
NSLog(#"Connected");
}
It looks like you're putting "Authorization: OAuth ..." in the body of the POST request instead of putting "OAuth ..." in the Authorization header.
Give this code a try instead (untested, sorry):
NSString* oauthVersion=#"1";
NSString* oauth_signature_method=#"PLAINTEXT";
NSString* authorizationHeader = [NSString stringWithFormat: #"OAuth oauth_version=\"%#\", oauth_signature_method=\"%#\", oauth_consumer_key=\"%#\", oauth_signature=\"%#&\"",oauthVersion,oauth_signature_method,APP_KEY,APP_SECRET];
NSMutableURLRequest* request= [[NSMutableURLRequest alloc]init];
[request setURL:[NSURL URLWithString:#"https://api.dropbox.com/1/oauth/request_token"]];
[request setHTTPMethod:#"POST"];
[request setValue:authorizationHeader forHTTPHeaderField:#"Authorization"];
NSURLConnection* conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[conn start];
Also, I would strongly suggest using OAuth 2 instead of OAuth 1. It's considerably simpler.
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.
Getting following error while consuming SOAP webservice in iOS App
"No Action header was found with namespace 'http://www.w3.org/2005/08/addressing' for the given message."
The same webservice working fine in SOAP UI Tool.
Following is the request format
NSString *data = #"<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tem=\"http://tempuri.org/\">
<soap:Header></soap:Header>
<soap:Body><tem:GetEvaluators></tem:GetEvaluators></soap:Body>
</soap:Envelope>";
NSString *url = #"webservice url";
NSData *postData = [data dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
[request setTimeoutInterval:20.0];
[request setValue:#"application/soap+xml;charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"http://tempuri.org/IATCService/GetEvaluators" forHTTPHeaderField:#"SOAPAction"];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Complete error response received from webservice
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/fault</a:Action>
</s:Header>
<s:Body>
<s:Fault>
<s:Code>
<s:Value>s:Sender</s:Value>
<s:Subcode>
<s:Value>a:MessageAddressingHeaderRequired</s:Value>
</s:Subcode>
</s:Code>
<s:Reason>
<s:Text xml:lang="en-US">No Action header was found with namespace 'http://www.w3.org/2005/08/addressing' for the given message.</s:Text>
</s:Reason>
<s:Detail>
<a:ProblemHeaderQName>a:Action</a:ProblemHeaderQName>
</s:Detail>
</s:Fault>
</s:Body>
Any help really appreciated.
We had the same problem with an ASP.NET-based server (error message when using python/suds, same query worked in SoapUi); after a lot of digging, we found that we need to add a SOAP header (as XML element) which contains the action; having the action in the Content-Type or SOAPAction headers was not enough (but does not hurt either). Here's an example of a successful query (from SoapUi):
<SOAP-ENV:Envelope xmlns:ns0="..." xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope">
<SOAP-ENV:Header xmlns:wsa="http://www.w3.org/2005/08/addressing"><wsa:Action>http://www.foo.com/.../SomeJob/getParameters</wsa:Action></SOAP-ENV:Header>
<ns1:Body>
<ns0:getParameters>...</ns0:getParameters>
</ns1:Body>
</SOAP-ENV:Envelope>
With Python and SUDS, this is how we did it:
from suds.sax.element import Element
wsans = ('wsa', "http://www.w3.org/2005/08/addressing")
client.set_options(soapheaders = Element('Action', ns=wsans).setText(action))
The action can be queried from the method, i.e. if you want to call a method client.service.foo, use
action = client.service.foo.method.soap.action
We found this by looking at the SoapUi HTTP log. (We also tried Wireshark, but that would not work because we're trying to use an https server which we don't own.)
Include
[request setValue:#"application/xml" forHTTPHeaderField:#"Content-Type"];
Finally i able to solve this problem, the content-type and request format are totally different than what i am passing with request
Content-Type: application/soap+xml;charset=UTF-8;action="http://tempuri.org/IATCService/GetEvaluators"
So difference is i have to pass action with content type.
Also the request Soap Envelop is totally different with different namespaces
<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\">
**<s:Header>
<a:Action s:mustUnderstand=\"1\">http://tempuri.org/IATCService/GetEvaluators</a:Action> </s:Header>**
<s:Body>
<GetEvaluators xmlns=\"http://tempuri.org/\"/></s:Body>
</s:Envelope>
As the header information is also totally different we have to specify action over there.
Hope it will be helpful to someone, who is facing same problem.
From my iPhone app, I'm making a call to an ASP.NET web service. Below is my code
NSString *urlString = #"http://tbtesting/teambinder5/KEY9XXXXXX-cXXe-49XX2-acca-2XXXXXXXa9/Dashboard/DashboardWidgets.asmx/GetAnnouncement";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"*/*" forHTTPHeaderField:#"Accept"];
[request setValue:#"application/json; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"en-us" forHTTPHeaderField:#"Accept-Language"];
[request setValue:#"gzip, deflate" forHTTPHeaderField:#"Accept-Encoding"];
[request setValue:#"20" forHTTPHeaderField:#"Content-Length"];
//[request setValue:#"3" forHTTPHeaderField:#"announceIntKey"];
NSError *error = nil;
NSURLResponse *response = [[NSURLResponse alloc] init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (error) {
NSLog(#"Following error occured: %#", error.localizedDescription);
}
else {
NSString *returnValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"message: %#", returnValue);
}
Here is the header taken from fiddler.
POST http://tbtesting/teambinder5/KEY9XXXXXX-cXXe-49XX2-acca-2XXXXXXXa9/Dashboard/DashboardWidgets.asmx/GetAnnouncement HTTP/1.1
Accept: */*
Accept-Language: en-us
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
Content-Length: 20
{"announceIntKey":3}
I'm getting the following error message.
"Message":"Invalid web service call, missing value for parameter: \u0027announceIntKey\u0027."
After a little bit of searching here on SO I found out the cause for this error. It is because I'm not passing the announceIntKey and its value.
Although I know the reason, I'm kinda clueless on how to correct it. Since it looks like an object literal, you can't pass it like any other header value. All the example code I came across here on SO is for jQuery or some other language.
My question is how can I pass this value in Objective C?
Thank you.
It looks like you're sending the content-type of application/json, so it would stand to reason that you need to pass the value as part of a POST containing a JSON body.
You will need to add that to the body of your request. You don't need to set the content-length as the NSMutableURLRequest will manage this for you.
NSString *body = "{\"announceIntKey\" : VALUE}";
[request setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding]];
You'll need to replace VALUE with what actually goes into the announceIntKey.
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>"]