如何将带有POST请求的参数发送到linkedIn api以在ios中发送消息

时间:2015-02-25 08:03:17

标签: ios objective-c xml post linkedin

我正在测试LinkedIn api发送消息。我可以使用accessToken登录,也可以从连接中获取联系人,但是当我尝试将消息发送到一个联系人ID时,我会收到未经授权的错误(401)并且无法发送消息。

这是我尝试的代码:

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://api.linkedin.com/v1/people/~/mailbox"]];
 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];

我收到此错误: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x16e356c0

我也尝试下面的代码:

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://api.linkedin.com/v1/people/~/mailbox"]];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc]initWithURL:url consumer:consumer token:accessTokenOAt callback:nil signatureProvider:nil];

NSData *myRequestData = [xml dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody: myRequestData ];
[request setHTTPMethod:@"POST"];

[request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
OADataFetcher *dataFetcher = [[OADataFetcher alloc]init];
[dataFetcher fetchDataWithRequest:request delegate:self didFinishSelector:@selector(networkApiCallResult:didFinish:) didFailSelector:@selector(networkApiCallResult:didFail:)];

然后我收到此错误:

"errorCode": 0, "message": "[unauthorized]. OAU:77re0739g6qxbx|8439199c-b9b3-48e3-b0f3-9a8dcd51bb8e|*01|*01:1424858697:/0A6nqLIKpAM8cok/sC2WrATUnE=", "requestId": "MO733L5NRZ", "status": 401, "timestamp": 1424858697907

为什么会这样?如何修复它才能发送消息?提前致谢。

0 个答案:

没有答案