简体   繁体   中英

LinkedIn Invitation API error 401 [unauthorized]-iPhone sdk

I am using OAuth-Sample-Client code to send invitation from email address via Linked Invitation API. I have created the same body as described in developer site,but right now I am getting error of 401,I have searched a lot of on it but didn't get any solution which get me out of it from any site or forum. My response is as below.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<error>
  <status>401</status>
  <timestamp>1351159386437</timestamp>
  <request-id>YX4NRU3S7J</request-id>
  <error-code>0</error-code>
  <message>[unauthorized]. OAU:ron8e8nko3te|e0a96317-dfdc-44fb-b427-5655e02f97ed|*01|*01:1351159294:dRCDN6b3K9F/mwWAaByKZQPgeVw=</message>
</error>

Here my json string would be like this:

{"body":"Say yes!","subject":"Invitation to connect.","recipients":{"values":[{"person":{"first-name":"test","last-name":"test","_path":"/people/email=test@test.com"}}]},"item-content":{"invitation-request":{"connect-type":"friend"}}}

I have used the code as below to send Invitation.

- (IBAction)postButton_TouchUp:(UIButton *)sender
{    
    [statusTextView resignFirstResponder];
    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=test@test.com",@"_path",@"test",@"first-name",@"test",@"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];
    NSLog(@"%@",[dict description]);
    NSString *updateString = [dict JSONString];
    [request setHTTPBodyWithString:updateString];
    [request setHTTPMethod:@"POST"];

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

I got stuck at this point,Please get me out of this. -Thanks in advance

call prepare method of OADataFetcher manually. Just put it before setting the body of the request.

[request prepare];
[request setHTTPBodyWithString:updateString];

This is working for me. Also remove the prepare method from the OADataFetcher when you are working with Invitation API.

Apple's code works! Just one more thing, dont forget any header. I was still getting the same message, turns out I forgot to add x-li-format and Content-type headers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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