LinkedIn Integration in iOS: Invitation API not wo

2019-02-15 11:41发布

问题:

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. 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. And I use the details from LinnkedIn API as well to implement this. Next, I need to send invitation to connect in LinkedIn from the same app. I tried using 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:

{"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"}}}

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. But ths issue is the target LinkedIn user didn't get any request!

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
2.linkedin connect API gives error 401 - iphone

and the NSURLResponse statuscode I get is 401 !

EDIT:: Solved it myself. See my answer below.

回答1:

Glad to inform all that after long 4 days of continuos research, I got this problem solved! I got the solution from this Linkedin thread

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

- (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