2

我正在尝试将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":"连接邀请。","re​​cipients":{"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 !

编辑:: 自己解决了。请看下面我的回答。

4

1 回答 1

5

很高兴地通知大家,经过长达 4 天的持续研究,我解决了这个问题!我从这个Linkedin 线程得到了解决方案

您只需要对代码进行一些更改:

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:nil];
    [request setHTTPMethod:@"POST"];
    NSString *messageToPerson = @"/people/email=target@gmail.com";
    NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,@"_path",@"TargetFirstName",@"first-name",@"TargetSecondName",@"last-name",nil] autorelease], @"person",nil]; 
    NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil];
    NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,@"values", nil];
    NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:@"friend",@"connect-type",nil] autorelease], @"invitation-request",nil];
    NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,@"item-content", nil];
    NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,@"recipients",@"Invitation",@"subject",@"ConnectWithMe",@"body", ir, @"item-content", nil];
    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    NSLog(@"%@",[update description]);
    NSString *updateString = [update JSONString];

    [request prepare];

    [request setHTTPBodyWithString:updateString];

    OADataFetcher *fetcher = [[OADataFetcher alloc] init];
    [fetcher fetchDataWithRequest:request delegate:self
        didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
        didFailSelector:@selector(postUpdateApiCallResult:didFail:) withId:1];

    [request release];

并且在oAdata OADataFetcher.m

- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector  withId:(int)idtm
    {

    [request release];
    
request = [aRequest retain];

    delegate = aDelegate;
    
didFinishSelector = finishSelector;

    didFailSelector = failSelector;

//note this change

    if (idtm!=1) {
   
    [request prepare];
    
       }

connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
    
}
    

试试这个。肯定会工作。希望这将有助于未来的游客。

问候

于 2013-04-10T06:51:12.450 回答