IPhone : accessing magento soap based api from ios app

on Monday, February 23, 2015

I am trying to access magento api from my iphone application. I have used this url


as reference but I am getting following error in xml as reponse:


2 Access denied.


My Code:



- (NSString *)createEnvelope:(NSString *)method forNamespace:(NSString *)ns forParameters:(NSString *)params{
NSMutableString *s = [NSMutableString string];
[s appendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];
[s appendFormat:@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"%@\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ns2=\"http://xml.apache.org/xml-soap\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">", ns];
[s appendString:@"<SOAP-ENV:Body>\n"];
[s appendFormat:@"<%@>%@</%@>", method, [params stringByReplacingOccurrencesOfString:@"&" withString:@"&amp;"], method];
[s appendString:@"</SOAP-ENV:Body>"];
[s appendString:@"</SOAP-ENV:Envelope>"];
return s;


}


-(void)login_request{ NSString *parameters = [NSString stringWithFormat:@"%@%@",kMagentoUsername,kMagentoApiKey]; NSString *envelope = [self createEnvelope:@"login" forNamespace:@"urn:Magento" forParameters:parameters];



NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://myurl.com/index.php/api/v2_soap/"]];
[request setHTTPMethod:@"POST"];

NSMutableDictionary *defaultHeaders = [[NSMutableDictionary alloc] init];
[defaultHeaders setValue:@"gzip" forKey:@"Accept-Encoding"];
[defaultHeaders setValue:@"en, en-us;q=0.8" forKey:@"Accept-Language"];
[defaultHeaders setValue:@"text/xml; charset=utf-8" forKey:@"Content-Type"];
[defaultHeaders setValue:@"catalogCategoryCurrentStore" forKey:@"SOAPAction"];
[defaultHeaders setValue:@"com.lognllc.Magento-iOS-Example/1.0.0 (unknown, iPhone OS 8.1, iPhone Simulator, Scale/2.000000)" forKey:@"User-Agent"];

[request setAllHTTPHeaderFields:defaultHeaders];
[request setHTTPBody:[envelope dataUsingEncoding:NSUTF8StringEncoding]];

NSURLResponse* response;
NSError* error = nil;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* newStr = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding];
NSLog(@"newStr %@",newStr);


}


Can someone please help me point out what mistake I am making in accessing the api?


Thanks

Pankaj


0 comments:

Post a Comment