Question

Hi I want share text in LinkedIn through my app. My code is below here...

This method when I click the btn linkedIn share..

 - (void)linkedBtnEvent
    {
    if(oAuthLoginView != nil) {

        oAuthLoginView.delegate = nil;
        oAuthLoginView = nil;
    }

    oAuthLoginView = [[OAuthLoginView alloc] initWithNibName:nil bundle:nil];
    oAuthLoginView.delegate=self;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginViewDidFinish:)
                                                 name:@"loginViewDidFinish"
                                               object:self.oAuthLoginView];

    [self presentViewController:self.oAuthLoginView animated:YES completion:nil];
}

This are the method to handle share after login..

-(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [self profileApiCall];
}

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

    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];

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

}

- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    NSString *responseBody = [[NSString alloc] initWithData:data
                                                   encoding:NSUTF8StringEncoding];

    NSDictionary *profile = [responseBody objectFromJSONString];

    if ( profile )
    {
        NSLog(@"%@", [[NSString alloc] initWithFormat:@"%@ %@",
                      [profile objectForKey:@"firstName"], [profile objectForKey:@"lastName"]]);
    }

    // The next thing we want to do is call the network updates
    [self networkApiCall];
}

- (void)profileApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

- (void)networkApiCall
{
    NSURL *url = [NSURL URLWithString:@"https://api.linkedin.com/v1/people/~/network/updates?scope=self&count=1&type=STAT"];
    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url
                                    consumer:oAuthLoginView.consumer
                                       token:oAuthLoginView.accessToken
                                    callback:nil
                           signatureProvider:nil];

    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];

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

}

- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    if (isSharedLinked)
    {
        NSLog(@"Shared Successfully");
        linkedBtn.enabled = NO;
    }
    else
    {
        isSharedLinked = YES;
        [self performSelector:@selector(postTextLinkedIn) withObject:nil afterDelay:1];
    }
}

- (void)networkApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

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

    NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [[NSDictionary alloc] 
                             initWithObjectsAndKeys:
                             @"anyone",@"code",nil], @"visibility", 
                            @"Wow its working... Share the text in Linked In", @"comment", 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:)];    
}

- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    // The next thing we want to do is call the network updates
    [self networkApiCall];
}

- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

It shows no error it always haow it share successfully in LinkedIn but no text share in that.. Please help me to fix this issue... I cant figure what mistake I done..

Was it helpful?

Solution

You need to add "rw_nus" in "scope". This only permit the app to share updates... This is your mistake otherwise all your code correct man..

- (void)requestTokenFromProvider
{
    OAMutableURLRequest *request = 
            [[[OAMutableURLRequest alloc] initWithURL:requestTokenURL
                                             consumer:self.consumer
                                                token:nil   
                                             callback:linkedInCallbackURL
                                    signatureProvider:nil] autorelease];

    [request setHTTPMethod:@"POST"];   

    OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:@"scope"
                                                                       value:@"r_fullprofile+r_contactinfo+r_emailaddress+r_network+r_basicprofile+rw_nus"];
    NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
    [request setParameters:params];
    OARequestParameter * scopeParameter=[OARequestParameter requestParameter:@"scope" value:@"r_fullprofile r_contactinfo r_emailaddress r_network r_fullprofile rw_nus"];

    [request setParameters:[NSArray arrayWithObject:scopeParameter]];

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

Replace this is in your oAuthloginView.m

OTHER TIPS

You can use a great lib for LinkedIn sharing: https://www.cocoacontrols.com/controls/linkedin-share

or directly from GitHub: https://github.com/pmilanez/MIS-Linkedin-Share

It is covered with MIT License and can be used in commercial project as well.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top