ios managing authentication tokens from NSURLRequest / HTTP request - iphone

When I make a POST request to sign in from within the iPhone app I am developing, the request returns an authentication token that I can successfully store in an NSString to be used throughout the rest of the app.
However, for any request after the sign in, I must pass it this authentication token so the request knows that a user is logged in. What is the complete way to do this for an NSURLRequest / what parameters/values must i specify for the NSURLRequest? Thank you in advance.

These NSURLRequest methods might help you
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"yoururl.com"]];
[request setHTTPMethod: #"POST" ];
[request setValue:#"yourtoken" forHTTPHeaderField:#"header field"];
[request setHTTPBody: dataifneeded ];

Related

Changes coming in Version 1.1 of the Twitter API in iPhone

I am using twitter API version V1.0 for my new iphone application .I am successfully able to do this using twitters v1.0 of the API and all works perfectly. Simply making a request to http://api.twitter.com/1/statuses/user_timeline.json?screen_name=userid retrieves all the information that I require.
since v1.0 has been deprecated and V1.1 requires authentication for each request, I get a bad authorization error (HTTP response status: 400)using this API.
What are the Changes i need to do in my appication .how to generate OAuth request headers,Do i need register my application ?How can i get authentication for new version?
I hope the above makes sense and any help would be appreciated.
Thanks in advance.
Visit https://dev.twitter.com/docs/ios/making-api-requests-slrequest
https://dev.twitter.com/docs/oauth/xauth
u can get the authentication using following method:
NSString *base64 = #"Basic NFRLWGlZY3l1aHJ4OVJaUWI5RW5BOmdVMXlvcVV6YzBNZUhUQmFXdVRZU2NtZHlIWDFOZXhwZmxqRE16bm01aw==";
//oauth_consumer_secret,oauth_consumer_key
NSURL *urlAPI = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/oauth2/token"]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:urlAPI];
[request setDelegate:self];
[request setRequestMethod:#"POST"];
[request addRequestHeader:#"Authorization" value:base64];
[request addRequestHeader:#"Content-Type" value:#"application/x-www-form-urlencoded;charset=UTF-8"];
[request setPostValue:#"client_credentials" forKey:#"grant_type"];
In response u will get the authtoken for your application like this
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAMddRQAAAAAAXRP6Axur1RS%2Fv9YFtJ7TijyPAGo%3DpmNowwOGf3dZTHDiH2gnCcv7qNIyGZcyV2IW5YFTBs"}

iPhone Objective C oauth 2.0 Login - unsupported_grant_type

I am brand new to salesforce development and am trying to connect to Salesforce to get the token.
I guess my question is 2 fold:
1) Do I have to use a webpage to authenticate with SF?
2) If not, why is this not working? When I try to use the username and password method of authenticating, I get:
{"error":"unsupported_grant_type","error_description":"grant type not supported"}
Here is my code:
NSString *post = [NSString stringWithFormat:#"grant_type=basic-credentials&client_id=%#&client_secret=%#&redirect_uri=%#",
kOAuthClientID,
kOAuthClientSecret,
kOAuthClientAuthURL
];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:kOAuthClientTokenUrl]];
[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;
NSLog(#"%#", request);
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#", data);
Any thoughts would be welcome.
I did try using grant_type = authorization_code but got another error:
{"error":"invalid_grant","error_description":"invalid authorization
code"}
If you want to use the username/password grant type, then its type is password not basic-credentials, the correct set of parameters to send are
grant_type=password
client_id=xxxxxxxxxx
client_secret=1234567890
username=noreply#salesforce.com
password=XXXXXXXXX
As i mentioned in your previous question, although this works, you really do want to use the web based flow so as to support salesforce customers that are using alternative login services like SAML.
1) Yes you do, with OAuth you need to sign into Salesforce and grant your application access. For an iOS app, you'd be looking at the client flow Salesforce OAuth Client Flow Doc
2) Take a look at the Toolkit for iOS on the Force.com developer site. There is a class called ZKOAuthViewController that pretty much does it all for you. The rest of the tooklit is based around Force.com's SOAP API but I just made my own OAuth login controller based on the toolkits and use the REST API instead.
Superfell's answer is nearly correct. The password needs to be set to:
password + security token
See http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_concepts_security.htm for extra info.

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?

How to use post manner to transmit username and password in order to log in a website on iphone or ipad platform?
Some one has suggest me that use ASIHTTPRequest,but I don't know how to use it.
Can somebody help me ?Thank you ........
ASIHTTPRequest has one of the best how to use pages of any library I have ever encountered. It is located here: http://allseeing-i.com/ASIHTTPRequest/How-to-use
If you need to post to a web form you could do something like this:
#define kURLString #"https://yourwebsite.com"
NSURL *url = [NSURL urlWithString:kURLString];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:#"myname" forKey:#"username"];
[request setPostValue:#"l33td00d" forKey:#"password"];
[request setDelegate:self];
[request startSynchronous];
Although in real life you may wish to run the request asynchronous. The details on how to do that are on the page I linked. It is defiantly worth reading.

Creating HTTPS login on iOS?

How do I build an https login on iOS? (Like this one: https://info.tam.ch/kks)
I only know how to solve a .htaccess.
Thank you.
I use ASIHTTPRequest in my iPhone project to communicate with the web application. For http authentication, they have a convenient ASIAuthenticationDialog class that takes care of that. Basic usage is as follows:
// Specifying a username and password to use in the URL
NSURL *url = [NSURL URLWithString:#"http://username:password#allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
// Setting a username and password to use on the request
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com/top_secret/"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUsername:#"username"];
[request setPassword:#"password"];
You can visit here for detailed information or check out the sample source code here on github.

Can I log in to my site from my iPhone app?

I'm trying to make a log in or sign up feature for my web site in my iPhone app. My website is a content management system, and like any other CMS, it has log in and registration features. It also has permmissions, dependent on the user account. I think I would have to use UIWebView for this.
Are there any examples or tutorials I can examine?
Check out the documentation for NSURLRequest (and NSMutableURLRequest): you can use it to make a POST request to your login and registration pages, just like a web browser. You can write the form UI in Cocoa/Objective-C and then send the data to the server.
As far as displaying the result to the user, you'll have to figure out a way to either parse the returned HTML (bad idea) or modify your CMS to return JSON or XML to iPhone requests (better idea).
Edit: Here's some sample code, taken from an app I'm working on (it submits data to Last.fm using POST):
NSURL *url = [NSURL URLWithString:#"http://example.com/"];
NSString *str = #"This is my example data!";
// everything below here is directly from my app:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:kLastFMClientUserAgent forHTTPHeaderField:#"User-Agent"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData];
[request setHTTPShouldHandleCookies:NO];
*connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];