How I do make an OAuth request using NSURLSession? - ios

I have a client ID and secret that I am supposed to send in a post request in order to receive a token to access an API (OAuth). I am using:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
to setup my authorization headers; however, I keep returning:
{
error = unauthorized;
"error_description" = "Full authentication is required to access this resource";
}
I have double and triple checked my client ID and secret, but I keep getting this response.
Here is the code that I am using to make my request:
NSURL *baseURL = [NSURL URLWithString:#"http://"];
NSURL *url = [NSURL URLWithString: #"/oauth/token?" relativeToURL:baseURL];
NSURL *absoluteURL = [url absoluteURL];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{#"Authorization" : [CLIENT_SECRET_PREFIX stringByAppendingString:[self base64Encode:CLIENT_SECRET_TOKEN]]};
sessionConfiguration.HTTPAdditionalHeaders = #{#"Content-Type" : #"application/json"};
NSURLSession *session =[NSURLSession sessionWithConfiguration:sessionConfiguration];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:absoluteURL];
[[session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(#"%#", json);
}] resume];
Can anyone please help me to solve this problem ? Thanks in Advance.

Related

NSURLSession send token back to server not working with ios10

I am trying to use NSURLSession, I had successfully received token from server, now I have to used that Token to get further details...
I had tried below code to get user data..
but I am getting httpResponse code: 500
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
NSString *authValue = [arrTokenData valueForKey:#"Token"];
//Configure session with common header fields
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{#"bearer": authValue};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSString *url = #"http://test.myserver.am/api/mobile/LookUps/getuserdata";
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
if (!error) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200)
{
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
//Process the data
}
}
}];
[task resume];
I am using iOS 10 , above code is not working, also I have tried several ways but no any success...
I have tried stackoverflow solution but this is also not working out for me...
Please help me out...

Not getting data from web service using NSURLSession with POST

When I try to call web service using NSURLSession with POST on Apple Watch, response parameter is nil. What might be the issue with this ?
Code for calling web service:
NSURL *url = [NSURL URLWithString:#"https://demo.test.com:22322/api/Alerts/UpdateAlertStatus?intUserId=5&paramaccessTokenId=24460c5f-be71-45b5-99cf-f46c277c3d9e&UserId=100200"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
NSMutableDictionary *mutDictParameters = [[NSMutableDictionary alloc] init];
[mutDictParameters setObject:#"708" forKey:#"AlertId"];
[mutDictParameters setObject:#"2878" forKey:#"XmlMessageId"];
[mutDictParameters setObject:#"5" forKey:#"UserId"];
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:mutDictParameters
options:kNilOptions error:&error];
if (!error) {
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
NSLog(#"Response : %#",response);
}];
[uploadTask resume];
}

Send NSString as body of post method using NSURLSession

I am trying to post a string to a server using the next example:
// 1
NSURL *url = [NSURL URLWithString:#"YOUR_WEBSERVICE_URL"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
// 3
NSDictionary *dictionary = #{#"key1": #"value1"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary
options:kNilOptions error:&error];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
}];
// 5
[uploadTask resume];
}
The difference is that I don't have a NSDictionary But an NSString object that stores an array of dictionaries, and also the string that I post must not be encoded it must be a simple string, so it is visible in the search field if I manually enter it.
My NSString example:
[{
"api_id" = debugger;
at = "2015-02-05T01:41:13Z";
oS = IOS;
ver = "8.10";
what = "showAdAt: forViewController:";
},
{
"api_id" = debugger;
at = "2015-02-05T01:41:13Z";
oS = IOS;
ver = "8.10";
what = "showAdAt: forViewController:";
}
]
Thank you in advance and be patient with me as this is my first post attempt.
I was thinking that the above example should work for me if I first convert the NSString to NSArray with dictionaries as objects.
UPDATE:
Currently I am trying to post the string as:
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"Authorization" : #"CUSTOM AUTHORIZATION THAT I AM USING",
#"Content-Type" : #"application/json"
};
// Create the session
// We can use the delegate to track upload progress
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];
// Data uploading task. We could use NSURLSessionUploadTask instead of NSURLSessionDataTask if we needed to support uploads in the background
NSURL *url = [NSURL URLWithString:#"MY WEBSITE LINK"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = #"POST";
//Convert the string to NSData
bodyContainerString = #"[{\"api_id\":\"A124\",\"at\":\"2011-04-10T20:09:31Z\",\"os\":\"ANDROID\",\"ver\":\"2.1\",\"what\":\"TEST\",\"value\":\"\"},{\"api_id\":\"A124\",\"at\":\"2011-04-10T20:10:31Z\",\"os\":\"ANDROID\",\"ver\":\"2.1\",\"what\":\"TEST\",\"value\":\"\"}]";
NSData* jsonData = [bodyContainerString dataUsingEncoding:NSUTF8StringEncoding];
jsonData = [jsonData subdataWithRange:NSMakeRange(0, [jsonData length] - 1)];
NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(newStr);
request.HTTPBody = jsonData;
NSURLSessionDataTask *uploadTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// Process the response
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
if (!error && httpResp.statusCode == 201) {
//if no error on upload then delete content of plist
NSLog(#"Success on post1");
}
NSLog(#"Success on post2");
}];
[uploadTask resume];
Update 2, the resulting link should look like:
curl -X POST http://MY_LINK/smtg -H 'Authorization: CUSTOM_FIRST_HEADER' -H "Content-Type: application/json" -d '[{"api_id":"A124","at":"2011-04-10T20:09:31Z","os":"ANDROID","ver":"2.1","what":"TEST","value":""},{"api_id":"A124","at":"2011-04-10T20:10:31Z","os":"ANDROID","ver":"2.1","what":"TEST","value":""}]'
I ended up using the first example that I found here is my implementation:
NSURL *url = [NSURL URLWithString:#"MY_LINK/smtg"];
//Create thhe session with custom configuration
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"Authorization" : [NSString stringWithFormat:#"BEARER %#",finalToken],
#"Content-Type" : #"application/json"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
// 2
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
// 3
NSError *error = nil;
NSData* jsonData = [bodyContainerString dataUsingEncoding:NSUTF8StringEncoding];
if (!error) {
// 4
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
fromData:jsonData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
// Handle response here
}];
// 5
[uploadTask resume];
}}

Minimal POST example for NSURLSession

I must be missing something basic because I am unable to get any NSURLSession examples using POST to work at all. I have my server set up to print out (to a file that I tail) all the received POST parameters and nothing I put in the POST body shows up. I've tried the solutions from Send POST request using NSURLSession as well as online tutorials such as the Ray Wenderlich Cookbook for using NSURLSession.
Here, for example, is the code almost directly from the Stackoverflow thread, mentioned above, with only the URL and the post arguments changed:
-(void)postTest {
NSString *textContent = #"XXXXX";
NSString *noteDataString = [NSString stringWithFormat:#"x=%#", textContent];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfiguration.HTTPAdditionalHeaders = #{
#"a" : #"YYYYY"
};
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
NSURL *url = [NSURL URLWithString:#"[MY URL with PHP script]"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPBody = [noteDataString dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPMethod = #"POST";
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
outputLabel.text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];
[postDataTask resume];
}
The PHP script shows the "XXXXX" parameter was properly received - but it's not part of the POST body; rather, it is part of the URL itself. The only parameter in the POST body is the "YYYYY" parameter but it doesn't show up at all.
The Ray Wenderlich example didn't work either: nothing showed up for the PHP script.
-(void)testPost {
NSURL *url = [NSURL URLWithString:#"[MY URL with PHP script]"];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
request.HTTPMethod = #"POST";
NSDictionary *dictionary = #{#"a": #"YYYYY"};
NSError *error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:kNilOptions error:&error];
if (!error) {
NSURLSessionUploadTask *uploadTask =
[session uploadTaskWithRequest:request
fromData:data completionHandler:^(NSData *data,NSURLResponse *response,NSError *error) {
}];
[uploadTask resume];
}
}
Is there something I'm not setting somewhere? I hadn't expected the shift to NSURLSession would have such subtle boobytraps and I'm wondering if it's something silly I'm doing wrong or missing. Thanks for any help!
Apple Documentation about the request parameter on uploadTaskWithRequest:fromData:completionHandler:
An NSURLRequest object that provides the URL, cache policy, request
type, and so on. The body stream and body data in this request object
are ignored.

NSURLSession send GET request even if I set POST in HTTPMethod

i'm trying to send a POST request to my server with the following code:
NSURLSessionConfiguration *defaultConfigObj = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration:defaultConfigObj];
//NSURL *url = [NSURL URLWithString:#"http://www.myserver.com/page.aspx"];
NSURL *url = [NSURL URLWithString:#"http://10.47.72.40/test/page.aspx"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.10f];
NSString *params = [NSString stringWithFormat:#"data=%#&UserMail=%#",
InfoDictionary[#"ID"],
arrDatosMail[0]
];
[urlRequest setHTTPMethod:#"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(#"Response: %# error:%#", response, error);
if(error == nil){
NSString *textRepsonse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Data: %#", textRepsonse);
}
}];
So, when a try with the local IP, it works fine (the server receives the POST request), but when I change the remote IP (with de www.myserver.com/page.aspx) the page receives the request with GET method, i don't know why :(,
What i'm doing wrong? Thanks in advance.
There is no problem. Request will be sent as POST.
I would recommend to use proxy to debug what is send exactly. On Mac Charles proxy is good one.
Charles proxy web site

Resources