ios invalid_request while calling for autherization token using oauth - oauth

I'm using google api to do google lattitude operations in my application. I have done till autherizing the user using credentials. Now i'm trying to generate accesstoken using the autherization_code generated from the initial request. But it is throughing invalid_request as error in the didReceiveData function. can some one of you please suggest me to solve my problem. Oauth request format is suppose to be like follows.
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
my ios code to perform the above service calll is follows
NSString * urlString = [NSString stringWithFormat:#"https://accounts.google.com/o/oauth2/token?code=%#&client_id=363756608022.apps.googleusercontent.com&client_secret=dury2bxloSVQ1sdJjg9MKO9s&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code",[userDefaults valueForKey:#"accessTokenResponse"]];
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url];
[request addValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"POST"];
NSLog(#"sendng req : %#",request);
NSURLConnection * connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
[connection start];

use asihttpheader to form header request. [request setpostvalue" forkey :]. this solved my problem

Related

How do I make an HTTPS request on iOS?

I need to connect to an API via a GET request. I have a string that will be the URL. What is the current best practice way on iOS 7 to send that GET request over HTTPS, and store the string in a string variable?
I just need to make a simple request. There is no session data involved. No cookies. Just a simple GET from a URL and it returns me a string in the response body. Thanks!
First you need to implement connection delegate methods.
Following is the code for making http request.
NSString *strURL = [NSString stringWithFormat:#"https://your_url"];
NSURL *url = [NSURL URLWithString:strURL];
NSLog(#"URL Request : \n %#",url);
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
[req addValue:#"your API Key" forHTTPHeaderField:#"Api-Key"];
[req setHTTPMethod:#"GET"];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
webData = [[NSMutableData data] retain];
}

I am implementing Oracle UCM in iPad application is there any web services for login authentication?

I am implementing Oracle UCM in iPad application is there any web services for login authentication. now i am using the j_security_check?.. i am unable to handle j_security_check response.
NSURL *url = [NSURL URLWithString:#"http://serveripaddress/cs/idcplg/j_security_check?"];
//initialize a request from url
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
//set http method
[request setHTTPMethod:#"POST"];
//initialize a post data
NSString *postData =[NSString stringWithFormat:#"j_username=%#&j_password=%#",usernameTF.text,passwordTF.text];
NSLog(#"userDetails : %#" ,postData);
//set request content type we MUST set this value.
[request setValue:#"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
//set post data of request
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *urlconnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = urlconnection;
You can use the iPad app for Webcenter Content rather than building your own app for the same . Please check the following links for app specific details :
http://docs.oracle.com/cd/E29542_01/doc.1111/e26695/mobile_integration.htm
https://itunes.apple.com/us/app/oracle-webcenter-content/id701199944?mt=8
Hope this helps .
Thanks,
Srinath

Setting parameter to HTTP request

I am creating an HTTP request. This is my request array where I set my method and my parameters:
...
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
//set headers
[request addValue:#"OAuth oauth_version=\"1.0\"" forHTTPHeaderField: #"Authorization"];
//create the body
NSMutableData *postBody = [NSMutableData data];
...
I know that the version is 1, the parameters are cursor and locale, and the method is POST
see https://www.dropbox.com/developers/core/api#delta
The response I receive is 403 or 0, instead of a JSON string. Is this the right way to set parameters? What should I do?
The 403 appears to be related the OAuth header.
Have you considered using the dropbox OAuth API's?
https://www.dropbox.com/developers/core/sdk
If not it appears you need to use the following technique:
https://www.dropbox.com/developers/blog/20

YouTube-Api Comment on videos

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.

Send JSON data to an URL

I have a certain URL "http://dev.mycompany.com" and I need to send some data (in JSON format) to it and get the response.
I can't find my way through the hugeload of documentation and related questions here on SO. I've managed to get the data (without sending any data) with NSURLConnection and it works good, the code I have so far is nearly the same as on https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html and so I don't see any point of posting the code I have.
I can't append some string to my URL as the data I need to send is JSON data. I am sorry if I sound like a noob, but I have very little experience in Obj-C and server communication.
You should send them as parameter to a post NSUrlRequest
As following:
NSURL *aUrl = [NSURL URLWithString:#"http://dev.mycompany.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:#"POST"];
NSString *postString = #"yourVarialbes=yourvalues";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request
delegate:self];
[connection start];
For sending JSON
Pelase read
How to send json data in the Http request using NSURLRequest

Resources