jeyben / IOSLinkedInAPI

Simple and non intrusive library to get access tokens for LinkedIn using Oauth2.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't share post with the LinkedIn API

mobihunterz opened this issue · comments

I have also registered the issue on stackoverflow: http://stackoverflow.com/questions/23758142/ioslinkedinapi-cant-share-post-with-the-linkedin-api
https://github.com/jeyben/IOSLinkedInAPI/issues/new

In this code I can't find proper example code but however I had written some code through which post can be shared. Here is my code:

In the code I have only single viewcontroller in which I have taken only two buttons, 1) Linked In Account [This button is used to present login controller and gets user successfully logged into the account] 2) Share [Allows user to share content on behalf of logged in user where the request fails]

ViewController.h

import <UIKit/UIKit.h>

import "LIALinkedInApplication.h"

import "LIALinkedInHttpClient.h"

@interface ViewController : UIViewController

@Property (nonatomic, strong) LIALinkedInHttpClient *client;

  • (IBAction) linkedInClicked:(id)sender;
  • (void)requestMeWithToken:(NSString *)accessToken;

@EnD

ViewController.m

import "ViewController.h"

@interface ViewController ()

@EnD

@implementation ViewController

  • (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"http://www.google.com" clientId:@"w57zqiw6cv73" clientSecret:@"Pj5MVxtkpbefau1v" state:@"something" grantedAccess:@[@"r_fullprofile", @"r_network", @"rw_nus"]];
    self.client = [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil];

}

  • (IBAction) linkedInClicked:(id)sender { // Login into the account
    [self.client getAuthorizationCode:^(NSString *code) {
    [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {
    NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
    [self requestMeWithToken:accessToken];
    } failure:^(NSError *error) {
    NSLog(@"Quering accessToken failed %@", error);
    }];
    } cancel:^{
    NSLog(@"Authorization was cancelled by user");
    } failure:^(NSError *error) {
    NSLog(@"Authorization failed %@", error);
    }];
    }

  • (IBAction) postMessage :(id)sender { // Post via logged in account, so, first go login and then share content
    NSString *strURL = @"https://api.linkedin.com/v1/people/~/shares";

    NSMutableDictionary *contents=[[NSMutableDictionary alloc] init];
    [contents setValue:@"description goes here" forKey:@"description"];
    [contents setValue:@"www.google.com" forKey:@"submitted-url"];
    [contents setValue:@"title goes here" forKey:@"title"];

    NSMutableDictionary *visible=[[NSMutableDictionary alloc] init];
    [visible setValue:@"anyone" forKey:@"code"];

    NSMutableDictionary *updatedic=[[NSMutableDictionary alloc] init];

    [updatedic setObject:visible forKey:@"visibility"];
    [updatedic setObject:contents forKey:@"content"];
    [updatedic setValue:@"Check out the LinkedIn Share API!" forKey:@"comment"];
    //[updatedic setValue:@"json" forKey: @"x-li-format"];

    [self.client POST:strURL parameters:updatedic success:^(AFHTTPRequestOperation *operation, NSDictionary *dict) {
    NSLog(@"Successfully posted", nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Failed post", nil);
    }];
    }

  • (void)requestMeWithToken:(NSString *)accessToken {
    [self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result) {
    NSLog(@"current user %@", result);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failed to fetch current user %@", error);
    }];
    }

When I try to login the app, I get successful message but after that when I try to share any post as described in above code, I get failure and see what is console:

Printing description of error:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x8a6d500 {NSErrorFailingURLKey=https://api.linkedin.com/v1/people/~/shares, NSLocalizedDescription=Request failed: unauthorized (401), NSUnderlyingError=0x8ab1bd0 "Request failed: unacceptable content-type: text/xml", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a1f5f0> { URL: https://api.linkedin.com/v1/people/~/shares } { status code: 401, headers {
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Type" = "text/xml;charset=UTF-8";
Date = "Tue, 20 May 2014 09:38:01 GMT";
Server = "Apache-Coyote/1.1";
"Transfer-Encoding" = Identity;
Vary = "*";
"Www-Authenticate" = "OAuth realm="https://api.linkedin.com\"";
"X-LI-UUID" = "wUQ+CTiK5WDItDrWLbZJFQ==";
"X-Li-Fabric" = "PROD-ELA4";
"X-Li-Pop" = "PROD-ELA4";
"x-li-format" = xml;
"x-li-request-id" = 30K08X3IL7;
} }}

I have tried searching a lot on AFNetworking, LinkedIn authorization, Unauthorized access, etc. but could not found anything over this. Let me know if anyone of you are aware of something about this or suggest me any other option for LinkedIn iPhone SDK.

I don't think this framework as trustable because no one is caring to answer an issue.

This is open source. Everybody is more than welcome to contribute to solve issues like this.
You can fork it, solve the issue and if you feel like to share with other users, make a pull request.

I am not maintaining this as a product in any way.

oh... ok

Not able solve this issue.. can any one help me ..

I would grap the exact post you re doing (using Charles proxy or something) and ask for help in the linkedIn forum as it looks like an issue in the concrete post towards the linkedin API and not something regarding obtaining the access_token:
https://developer.linkedin.com/forum/rwnus-permission-and-statusactivity-posting

@ Jacob Thanks for your reply. I tried different ways but i dint find any solution.