Search code examples
iphonexcodeios5linkedin-api

LinkedIn starter kit not sharing content to wall If add content dictionary?


I have written code to post content to linkedIn wall and it is working fine if I remove content dictionary from update dictionary. See the below code containing post data:

- (IBAction)postUpdate
    {
     NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
     OAMutableURLRequest *request =
     [[OAMutableURLRequest alloc] initWithURL:url
                                     consumer:oAuthLoginView.consumer
                                        token:oAuthLoginView.accessToken
                                     callback:nil
                            signatureProvider:nil];

     NSDictionary *content=[[NSDictionary alloc] initWithObjectsAndKeys:@"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg",@"submitted-image-url",@"http://www.youtube.com/watch?v=GoZ2Be2zLq8",@"submitted-url",@"Post Image and Video testing",@"title",@"Posted Description",@"description",nil];

     NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
                             [[NSDictionary alloc]
                              initWithObjectsAndKeys:
                              @"anyone",@"code",nil], @"visibility",
                             @"Test posting to linkedIn", @"comment",content,@"content",nil];


     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
     NSString *updateString = [update 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:)];
    }

Post data comes in didfinishSelector is :

data after posting is:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <error>
      <status>401</status>
      <timestamp>1356601471318</timestamp>
      <request-id>MFW58DCKE9</request-id>
      <error-code>0</error-code>
      <message>[unauthorized]. OAU:0onill9cburx|3c05c306-aad8-4d07-a2a1-2430aa21b54a|*01|*01:1356601465:Ji7pimMqrXp3RHCNJLv8iKZsklk=</message>
    </error>

I don't know why so? please help.. Thanks in Advance..


Solution

  • I have solved it

    working code to post on LinkedIn as follows:

    - (IBAction)postUpdate
    {
     NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
     OAMutableURLRequest *request =
     [[OAMutableURLRequest alloc] initWithURL:url
                                     consumer:oAuthLoginView.consumer
                                        token:oAuthLoginView.accessToken
                                     callback:nil
                            signatureProvider:nil];
    
     NSMutableDictionary *contentDic=[[NSMutableDictionary alloc] init];
    
     [contentDic setValue:@"http://www.celebs101.com/wallpapers/Bruce_Lee/421101/Bruce_Lee_Wallpaper.jpg" forKey:@"submitted-image-url"];
     [contentDic setValue:@"http://www.linkedin.com" forKey:@"submitted-url"];
     [contentDic setValue:[NSString stringWithFormat:@"A title for your share"] forKey:@"title"];
    
    
     NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
                             [[NSDictionary alloc]
                              initWithObjectsAndKeys:
                              @"anyone",@"code",nil], @"visibility",
                             @"Test posting to linkedIn",@"comment",contentDic,@"content",nil];
    
     [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
     NSString *updateString = [update JSONString];
    
    
     NSLog(@"json string is %@",updateString);
    
     [request setHTTPBodyWithString:updateString];
     [request setHTTPMethod:@"POST"];
    
     OADataFetcher *fetcher = [[OADataFetcher alloc] init];
    
     [fetcher fetchDataWithRequest:request
                          delegate:self
                 didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
                   didFailSelector:@selector(postUpdateApiCallResult:didFail:)];
    }
    
    
    - (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
    {
     NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     NSLog(@"data after posting is %@",myString);
    }
    
    
    - (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error
    {
     NSLog(@"%@",[error description]);
    }