I am working on a Mac app in objective-c which will use the eBay API to access the eBay functionality, including trading. I have successfully retrieved items and categories, but for trading, I need the session ID and token, which I have not been able to do.
I have made many changes after reading various posts and the documentation, but always get this error after responding to the challenge:
error: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x6080000585a0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=https://api.sandbox.ebay.com/ws/api.dll, NSErrorFailingURLKey=https://api.sandbox.ebay.com/ws/api.dll, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}
After making the call, I get the challenge callback and handle it here:
- (void)URLSession:(NSURLSession *)session
didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,
NSURLCredential *credential))completionHandler{
NSLog(@"didReceiveChallenge: %@", challenge);
NSURLCredential* credential = [NSURLCredential credentialWithUser:sandBoxUserID password:sandBoxPassword persistence:NSURLCredentialPersistenceForSession];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
The call for the session ID is here:
-(void) retrieveSessionID{
// retrieve a session ID for this user for this app
// headers for auth
NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSMutableString* urlString = [[NSMutableString alloc]initWithCapacity:200];
[urlString appendString:sandBoxEndPoint];
NSString* callname = @"GetSessionID";
NSString* version = @"768";
NSString* contentType = @"text/xml";
NSString* siteID = @"0";
NSMutableDictionary* headersDictionary = [[NSMutableDictionary alloc]initWithCapacity:10];
[headersDictionary setObject:contentType forKey:@"Content-Type"];
[headersDictionary setObject:sandBoxAppID forKey:@"X-EBAY-API-APP-NAME"];
[headersDictionary setObject:devID forKey:@"X-EBAY-API-DEV-NAME"];
[headersDictionary setObject:sandBoxCertID forKey:@"X-EBAY-API-CERT-NAME"];
[headersDictionary setObject:callname forKey:@"X-EBAY-API-CALL-NAME"];
[headersDictionary setObject:version forKey:@"X-EBAY-API-COMPATIBILITY-LEVEL"];
[headersDictionary setObject:siteID forKey:@"X-EBAY-API-SITEID"];
configuration.HTTPAdditionalHeaders = headersDictionary;
NSLog(@"configuration.HTTPAdditionalHeaders: %@", configuration.HTTPAdditionalHeaders);
NSLog(@"urlString: %@", urlString);
NSURL *eBayUrl = [NSURL URLWithString:urlString];
NSLog(@"eBayUrl: %@", eBayUrl);
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:eBayUrl];
request.HTTPMethod = @"GET";
NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"GetSessionIDRequest"];
[root addAttribute:[NSXMLNode attributeWithName:@"xmnls" stringValue:@"urn:ebay:apis:eBLBaseComponents"]];
NSXMLElement *childElement1 = [[NSXMLElement alloc] initWithName:@"RuName"];
childElement1.stringValue = sandBoxRuName;
[root addChild:childElement1];
NSXMLDocument *xmlRequest = [NSXMLDocument documentWithRootElement:root];
NSData* xmlRequestAsData = [xmlRequest XMLData];
request.HTTPBody = xmlRequestAsData;
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data,
NSURLResponse *response,
NSError *error) {
NSLog(@"error: %@", error);
NSLog(@"response: %@", response);
self.sessionIDRequestXMLstring = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"self.sessionIDRequestXMLstring: %@", self.sessionIDRequestXMLstring);
[self.delegate receivedSessionIDRequestXMLstring:self.sessionIDRequestXMLstring];
}
];
[dataTask resume];
}
I apologize for the formatting here, but I haven't quite figured out how to use it correctly.