How to pass authorization token header objective c? - objective-c

I'm passing username,password for an API call it returns right value. But after authentication API returns an security token I need to capture the token and pass along with username and password.I'm doing the same but it returns forbidden error which denotes invalid token error.I also tried base64 to pass token as suggested at some answer in stackoverflow.The code I use to pass header values below
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[self HTTPMethod]];
[request setHTTPBody:self.requestData];
[request addValue:#"xyz" forHTTPHeaderField:#"username"];
[request addValue:#"xyz" forHTTPHeaderField:#"password"];
[request addValue:[[NSUserDefaults standardUserDefaults]valueForKey:#"Auth"] forHTTPHeaderField:#"Authorization"];
[request addValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
I searched for header methods, But nothing helps.what I'm doing wrong here.pls anybody help me fix it.Thanks.

Use setValue for request instead of addValue because with addValue If a value was previously set for the specified field, the supplied value is appended to the existing value using the appropriate field delimiter
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:[self HTTPMethod]];
[request setHTTPBody:self.requestData];
[request setValue:#"xyz" forHTTPHeaderField:#"username"];
[request setValue:#"xyz" forHTTPHeaderField:#"password"];
[request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:#"Auth"] forHTTPHeaderField:#"Authorization"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
check apple documentation-
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableURLRequest_Class/#//apple_ref/occ/instm/NSMutableURLRequest/setValue:forHTTPHeaderField

Related

FBSDKAccessToken access denied Tinder API

I'm trying to get the authentication token of Tinder API by sending FB token like described in following documentation:
https://gist.github.com/rtt/10403467
I'm using objective c and I'm getting the fb access token from FBSDK. The response says me only that access is denied so I think there is something wrong with the fb access token. The facebook test user account I'm using is also already registered on Tinder.
What is wrong with this?
FB token:
FBSDKAccessToken *token = [FBSDKAccessToken currentAccessToken];
This is my request:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:#"https://api.gotinder.com/auth"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"application/json" forHTTPHeaderField:#"Accept"];
[request setValue:#"Tinder/6.1.0 (iPhone; iOS 9.3; Scale/2.00)" forHTTPHeaderField:#"User-Agent"];
[request setValue:#"ios" forHTTPHeaderField:#"platform"];
[request setValue:#"3" forHTTPHeaderField:#"app-version"];
[request setHTTPBody:postdata];

How to post data into a specific url or server

I want to build up a login page and the values of this page i want to submit in server through a specific link. can any one send the code that how i will send the data into server?
Here's an example using NSURLConnection to send POST parameters:
// Note that the URL is the "action" URL parameter from the form.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL #"http://www.whereq.com:8079/answer.m"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
//this is hard coded based on your suggested values, obviously you'd probably need to make this more dynamic based on your application's specific data to send
NSString *postString = #"paperid=6&q77=2&q80=blah";
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%u", [data length]] forHTTPHeaderField:#"Content-Length"];
[NSURLConnection connectionWithRequest:request delegate:self];

setValue: forHTTPHeaderField:#"Authorization" does not work

I'm trying to connect to a server which requires a username and password for Authentication. My code is the following:
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:#"username:password" forHTTPHeaderField:#"Authorization"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
But this does not work, I always get an error message:
title>401 Authorization Required</title>
</head><body>
<h1>Authorization Required</h1>
<p>This server could not verify that you
are authorized to access the document
requested. Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
When I try to connect to a server which doesn't nee an Authentication this code code works great. So, how to set the username and password correctly?
You should specify the domain of server, try this format:
NSString *authen = [NSString stringWithFormat:#"%#\\%#:%#", m_domain,
m_account, m_password];
[request setValue:authen forHTTPHeaderField:#"Authorization"];
this work for me!

NSURLRequest setValue: forHTTPHeaderField:#"Authorization" not working

I try to make an authenticate call to the twitter API with the Application only Authentication API
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:20.0];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField:#"Content-Type"];
[request setValue:#"Some App Name" forHTTPHeaderField:#"User-Agent"];
[request setValue:authValue forHTTPHeaderField:#"Authorization"];
[request setHTTPBody:[#"grant_type=client_credentials" dataUsingEncoding:NSUTF8StringEncoding]];
Interestingly the authValue is not getting set for HTTPHeaderField #"Authorization". Therefor my calls fail of course. All other header fields are set correctly.
I check it by
NSLog(#"Authorization, %#, %#", [request valueForHTTPHeaderField:#"Authorization"], authValue);
which returns #"Authorization, (null), theCorrectStuff
Why is this happening? I should be able to set it right away, or am I missing something?
Thanks for your time and consideration,
Fabian
I had the same issue. Output your authValue, it probably contains new lines. For the application only authentication after you do base64 you need to remove new lines from authValue otherwise the header field won't be set.
Or just use already written Objective-C library for Twitter REST API 1.1 (https://github.com/nst/STTwitter).

HTTP form POST method to server not sending email

Im trying to send a data or what it seems to a form that is on the website, the action is freecontactformprocess.php and my code goes like this,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.irabwah.com/freecontactformprocess.php"]];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"content-type"];
NSString *postString = [NSString stringWithFormat:#"Full_Name=%#&Email_Address=%#&Telephone_Number=%#&Your_Message=%#&AntiSpam=25",_fNameLabel.text, _emailLabel.text,_phoneLabel.text, _commentText.text];
NSData *data = [postString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:[NSString stringWithFormat:#"%u", [data length]] forHTTPHeaderField:#"Content-Length"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
if(conn){
NSLog("Successfull!!");
}
the purpose of this form is to send an email into my account which I integrated into the form on installation of this plugin, but the thing is I doesn't seems to get a the email when I run my test app, and when Im inputing values I don't include spaces yet.
The form looks like this,