我正在尝试将LinkedIn集成到我的 iOS 应用程序中,我需要在其中进行身份验证,然后获取个人资料详细信息并从我的应用程序发布到 LinkedIn。我使用这个示例项目LinkedIn OAuth 示例客户端作为参考非常成功地完成了所有这些事情,我做了一些修改,一切都运行良好。我也使用来自 LinnkedIn API的详细信息来实现这一点。接下来,我需要从同一个应用程序发送邀请以在 LinkedIn 中进行连接。我尝试使用Invitataion API。
这是我的代码片段:
-(void)sendInvitationToconnectTo:(NSString *)emailAddress withMessage:(NSString *)text
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/mailbox"];
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:oAuthLoginView.consumer
token:oAuthLoginView.accessToken
callback:nil
signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:@"/people/email=userName@gmail.com",@"_path",@"userName1",@"first-name",@"userName2",@"last-name", nil];
NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,@"person",nil];
NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil];
NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,@"values", nil];
NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:@"friend",@"connect-type",nil];
NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,@"invitation-request", nil];
NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,@"item-content",@"Say yes!",@"body",@"Invitation to connect.",@"subject",value,@"recipients", nil];
NSString *updateString = [dict JSONString];
NSLog(@"updateString : %@",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:@"POST"];
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(sendRequestApiCallResult:didFinish:)
didFailSelector:@selector(sendRequestApiCallResult:didFail:)];
[request release];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSLog(@"invitation sent");
[self networkApiCall];
}
- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
{
NSLog(@"%@",[error description]);
}
我的 json 字符串如下所示:
{"body":"说是!","subject":"连接邀请。","recipients":{"values":[{"person":{"first-name":"userName1","last -name":"userName2","_path":"/people/email=userName@gmail.com"}}]},"item-content":{"invitation-request":{"connect-type":"朋友”}}}
在发送邀请时,我没有收到任何错误,并且在调试时,该sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
方法被调用,显示邀请发送的文本。但是这个问题是目标LinkedIn用户没有收到任何请求!
任何人都可以帮我解决这个问题吗?我的代码中有什么做错了吗?
注意我通过下面提到的两个链接来获得解决方案,尝试了解决方案,但徒劳无功。
1.邀请 API - 被 401(iOS 和 OAuthConsumer)难住 - 已解决 2.linkedin
连接API 给出错误 401 - iphone
我得到的NSURLResponse
状态码是 401 !
编辑:: 自己解决了。请看下面我的回答。