简体   繁体   English

iOS中的LinkedIn集成:邀请API无效

[英]LinkedIn Integration in iOS: Invitation API not working

I'm trying to integrate LinkedIn in my iOS application, where I need to Authenticate, then fetch profile details and post to LinkedIn from my app. 我正在尝试将LinkedIn集成到需要进行身份验证的iOS应用程序中,然后获取个人资料详细信息并从我的应用程序发布到LinkedIn。 I do all these things quite successfully using this sample project LinkedIn OAuth Sample Client as reference, I did some modifications and everything is working quite well. 我使用此示例项目LinkedIn OAuth示例客户端作为参考,非常成功地完成了所有这些操作,我进行了一些修改,一切工作都很好。 And I use the details from LinnkedIn API as well to implement this. 我也使用LinnkedIn API的细节来实现这一点。 Next, I need to send invitation to connect in LinkedIn from the same app. 接下来, 我需要发送邀请以从同一应用程序连接到LinkedIn I tried using Invitataion API . 我尝试使用Invitataion API

Here is my code snippet: 这是我的代码段:

-(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]);
}

And my json string looks like this: 我的json字符串看起来像这样:

{"body":"Say yes!","subject":"Invitation to connect.","recipients":{"values":[{"person":{"first-name":"userName1","last-name":"userName2","_path":"/people/email=userName@gmail.com"}}]},"item-content":{"invitation-request":{"connect-type":"friend"}}} {“ body”:“ Say yes!”,“ subject”:“邀请连接。”,“收件人”:{“ values”:[{“ person”:{“名字”:“ userName1”,“最后-name “:” USERNAME2" , “_路径”: “/ people/email=userName@gmail.com”}}]}, “项内容”:{ “邀请请求”:{ “连接型”:”朋友“}}}

While sending the invitation I don't get any errors and when I debug, the sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data method gets called showing invitation sent text. 发送邀请时, 我没有任何错误,并且在调试时, sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data方法被调用,显示邀请发送的文本。 But ths issue is the target LinkedIn user didn't get any request! 但是,问题是目标LinkedIn用户没有收到任何请求!

Can anyone help me regarding this? 有人可以帮我吗? Is there anything I'm doing wrong in my code? 我的代码中有什么地方做错了吗?

NB I went through both the below mentioned links to get a solution, tried the solutions , but in vain. 注意:我通过下面提到的两个链接来获取解决方案,尝试了这些解决方案,但徒劳无功。

1. Invitation API - Stumped by 401 (iOS and OAuthConsumer) - SOLVED 1. 邀请API-被401(iOS和OAuthConsumer)困扰-已解决
2. linkedin connect API gives error 401 - iphone 2.linkedin Connect API给出错误401-iPhone

and the NSURLResponse statuscode I get is 401 ! 我得到的NSURLResponse是401!

EDIT:: Solved it myself. 编辑::我自己解决了。 See my answer below. 请参阅下面的答案。

Glad to inform all that after long 4 days of continuos research, I got this problem solved! 很高兴通知大家,经过长达4天的持续研究,我解决了这个问题! I got the solution from this Linkedin thread 我从Linkedin线程中获得了解决方案

You just need to make a few changes in the code: 您只需要在代码中进行一些更改:

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];

and in oAdata OADataFetcher.m 并在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];
    
}
    

Try this. 尝试这个。 Will work for sure. 肯定会工作。 Hope this will help future visitors. 希望这对将来的访客有帮助。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM