Wednesday, 25 February 2015

How to send parameters with POST request to linkedIn api to send message in ios



I am testing LinkedIn api to send message. I can sign in with accessToken and also can get contacts from connections, but when I try to send message to one contact id, I get error of unauthorized (401) and fail to send message.


This is the codes that I try:



NSString *xml;
xml=[NSString stringWithFormat:@"<?xml version='1.0' encoding='UTF-8'?><mailbox-item><recipients><recipient><person path='/people/~'></person></recipient><recipient><person path=\"/people/%@\"></person></recipient></recipients><subject>test</subject><body>%@</body></mailbox-item>",contactId,message];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://ift.tt/1iIwetW"]];
NSString *newUrl = [NSString stringWithFormat:@"%@?oauth2_access_token=%@", url,accessToken];

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFXMLParserResponseSerializer serializer];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:newUrl]];
[request setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[request setValue:@"xml" forHTTPHeaderField:@"x-li-format"];

[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Accept"];

NSOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSXMLParser *parser = responseObject;
parser.delegate = self;

if (![parser parse]) {
// handle parsing error here
} else {
// use parsed data here
NSLog(@"success : %@",parser);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// handle network related errors here
NSLog(@"error : %@",error);
}];

[manager.operationQueue addOperation:operation];


I get this error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x16e356c0


Why is it like this ? How can I fix it to be able to send message ? Thanks beforehand.


No comments:

Post a Comment