0

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.

SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
Rick Schlueter
  • 141
  • 1
  • 10
  • Exact same get request working on a regular browser? also check this out http://stackoverflow.com/questions/25372318/error-domain-nsurlerrordomain-code-1005-the-network-connection-was-lost – SpaceDust__ Dec 04 '15 at 18:41
  • How would I create the URL in a browser? Would I specify the headers as params such as: ?X-EBAY-API-SITEID=0&X-EBAY-API-DEV-NAME=abd, etc? – Rick Schlueter Dec 05 '15 at 00:25
  • After lots of research, I am still unable to resolve this. I think it may be related more to the OS X networking than eBay specifically, so I've logged an issue at the Apple Dev Forum. Maybe somebody there can help. – Rick Schlueter Dec 05 '15 at 17:06
  • @RickSchlueter this is an old post but I happened upon it today. The auth token flow http://developer.ebay.com/devzone/guides/ebayfeatures/Basics/Tokens-MultipleUsers.html is designed for a web app really. The accept/reject url parameters you enter on your developer dashboard for the user token flow consent form are only going to go to a URL called by the ebay servers. The link talks about how for a desktop app. We have this go to our apps web server url, and then we have our objective-C app make a private web service call to continue the FetchToken flow, e.g. – drshock Feb 17 '16 at 20:02

0 Answers0