Reputation: 334
I'm working on an iOS app using OAuth2 authentication.
I'm authenticated, I have no problem requesting my API, but I'm facing a problem when trying to download some files from protected URLS.
I'm using downloadTaskWithRequest
from AFURLSessionManager
(AFNetworking). It returns always an error 401 (not authorized) as if I was not logged in.
I can access this URL with no problem if I use the GET method, but I would like to use downloadTaskWithRequest
because it permits me to have a progress indicator.
Here's my code (which isn't working):
NSURLSessionDownloadTask *downloadTask = [apiC downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *dstPath = [NSURL fileURLWithPath:dstFilePath];
return dstPath;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
// HERE I HAVE A 401
}];
Here's my code that does work:
[[OEOApiClient sharedClient] GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
// SUCCEDD !!
} failure:^(NSURLSessionDataTask *task, NSError *error) {
}];
Does downloadTaskWithRequest
not support authentication? Am I doing something wrong?
Upvotes: 1
Views: 2182
Reputation: 334
OK, I was creating my request the wrong way :
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
this was the good way :
NSURLRequest *request = [mySessionManager.requestSerializer requestWithMethod:@"GET" URLString:url parameters:nil error:&serializationError];
Upvotes: 4
Reputation: 66244
The request
you're creating and passing to downloadTaskWithRequest:…
is different from the one generated by your request serializer in GET:parameters:…
.
Look at the implementation of GET:parameters:…
to learn how AFNetworking generates requests using request serializers. Then use the same code to make an authenticated request object to pass to downloadTaskWithRequest:…
.
Upvotes: 0