LinkedIn Integration In iOS App
Step 1 : Register your App
First step that you need to follow is to register you application in linkedIn.For this you need to have a LinkedIn Account.Create one if you don't have and register your App
This unique key helps us identify your application and lets you make API calls. Once you've registered your LinkedIn app, you will be provided with an API Key and Secret Key. For the safety of your application please do not share your Secret Key.
Step 2 :Create Project
Next thing you need to do is to create a new Xcode App and integrate OAuth files in it.
Step 3 : Get an access token
Access token is unique to a user and an API Key. You need access tokens in order to make API calls to LinkedIn on behalf of the user who authorized your application. Follow the two steps below to get one:
a. Generate Authorization Code by redirecting user to LinkedIn's authorization dialog
https://www.linkedin.com/uas/oauth2/authorization?response_type=code
&client_id=YOUR_API_KEY
&scope=SCOPE
&state=STATE
&redirect_uri=YOUR_REDIRECT_URI
Make sure that you are using https for the request or you will get an error.
If the user authorizes your application they will be redirected to the redirect_uri that you specified in your request above along with a temporary authorization_code and the same state that you passed in the request above.
Upon successful authorization, the redirected URL should look like:
YOUR_REDIRECT_URI/?code=AUTHORIZATION_CODE&state=STATE
b.Request Access Token by exchanging the authorization_code for it
POST
https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
&code=AUTHORIZATION_CODE
&redirect_uri=YOUR_REDIRECT_URI
&client_id=YOUR_API_KEY
&client_secret=YOUR_SECRET_KEY
Step 4. Make the API calls
You can now use this access_token to make API calls on behalf of this user by appending "oauth2_access_token=access_token" at the end of the API call that you wish to make.
GET:
https://api.linkedin.com/v1/people/~?oauth2_access_token=AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR
Access tokens have a life span of 60 days.
Please find some examples below of Get and Post that I have used in my Application :
1.) Get Profile Data:
For getting Profile you need to make a request to profile Api which looks like this:
- (void)profileApiCall
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.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:)];
}
On successful response, FinishSelector will get called that have code something like:
- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *profileData = [responseBody objectFromJSONString];
if ((profileData)&&(!isImagePost))
{
self.name.text = [[NSString alloc] initWithFormat:@"%@ %@",
[profileData objectForKey:@"firstName"], [profileData objectForKey:@"lastName"]];
self.headline.text = [profileData objectForKey:@"headline"];
}
// The next thing we want to do is call the network updates
[self networkApiCall];
}
Here in code I am retrieving name and headline in label.
2.) Post Status:
Again for posting status url request is being made.Find the code below:
- (IBAction)postButton_TouchUp:(UIButton *)sender
{
//create url request
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.accessToken
callback:nil
signatureProvider:nil];
//create dictionary with status that need to be posted
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
@"anyone",@"code",nil], @"visibility",
self.statusText.text, @"comment", nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//update JSON String
NSString *updateString = [update JSONString];
NSLog(@"json string is %@",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:@"POST"];
//Call OAuth method to post status and call bak methods
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
didFailSelector:@selector(postUpdateApiCallResult:didFail:)];
}
3.) Get Recent Status:
On successful post,the next thing you need to do is to call the network update for getting recent post.For this you need to make request to networkApi somewhat like :
- (void)networkApiCall
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/network/updates?scope=self&count=1&type=SHAR"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.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:)];
}
Based on network Api response ,data will be fetched.On Success,you need to code like this this to get current post :
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *person = [[[[[responseBody objectFromJSONString]
objectForKey:@"values"]
objectAtIndex:0]
objectForKey:@"updateContent"]
objectForKey:@"person"];
if ( [person objectForKey:@"currentShare"] )
{
self.status.text = [[person objectForKey:@"currentShare" ]
objectForKey:@"comment"];
}
}
This are some operations to play with linked In. You can try some others also.
Happy Coding….!!!
Step 1 : Register your App
First step that you need to follow is to register you application in linkedIn.For this you need to have a LinkedIn Account.Create one if you don't have and register your App
This unique key helps us identify your application and lets you make API calls. Once you've registered your LinkedIn app, you will be provided with an API Key and Secret Key. For the safety of your application please do not share your Secret Key.
Step 2 :Create Project
Next thing you need to do is to create a new Xcode App and integrate OAuth files in it.
Step 3 : Get an access token
Access token is unique to a user and an API Key. You need access tokens in order to make API calls to LinkedIn on behalf of the user who authorized your application. Follow the two steps below to get one:
a. Generate Authorization Code by redirecting user to LinkedIn's authorization dialog
https://www.linkedin.com/uas/oauth2/authorization?response_type=code
&client_id=YOUR_API_KEY
&scope=SCOPE
&state=STATE
&redirect_uri=YOUR_REDIRECT_URI
Make sure that you are using https for the request or you will get an error.
If the user authorizes your application they will be redirected to the redirect_uri that you specified in your request above along with a temporary authorization_code and the same state that you passed in the request above.
Upon successful authorization, the redirected URL should look like:
YOUR_REDIRECT_URI/?code=AUTHORIZATION_CODE&state=STATE
b.Request Access Token by exchanging the authorization_code for it
POST
https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code
&code=AUTHORIZATION_CODE
&redirect_uri=YOUR_REDIRECT_URI
&client_id=YOUR_API_KEY
&client_secret=YOUR_SECRET_KEY
Step 4. Make the API calls
You can now use this access_token to make API calls on behalf of this user by appending "oauth2_access_token=access_token" at the end of the API call that you wish to make.
GET:
https://api.linkedin.com/v1/people/~?oauth2_access_token=AQXdSP_W41_UPs5ioT_t8HESyODB4FqbkJ8LrV_5mff4gPODzOYR
Access tokens have a life span of 60 days.
Please find some examples below of Get and Post that I have used in my Application :
1.) Get Profile Data:
For getting Profile you need to make a request to profile Api which looks like this:
- (void)profileApiCall
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.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:)];
}
On successful response, FinishSelector will get called that have code something like:
- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *profileData = [responseBody objectFromJSONString];
if ((profileData)&&(!isImagePost))
{
self.name.text = [[NSString alloc] initWithFormat:@"%@ %@",
[profileData objectForKey:@"firstName"], [profileData objectForKey:@"lastName"]];
self.headline.text = [profileData objectForKey:@"headline"];
}
// The next thing we want to do is call the network updates
[self networkApiCall];
}
Here in code I am retrieving name and headline in label.
2.) Post Status:
Again for posting status url request is being made.Find the code below:
- (IBAction)postButton_TouchUp:(UIButton *)sender
{
//create url request
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.accessToken
callback:nil
signatureProvider:nil];
//create dictionary with status that need to be posted
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc]
initWithObjectsAndKeys:
@"anyone",@"code",nil], @"visibility",
self.statusText.text, @"comment", nil];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
//update JSON String
NSString *updateString = [update JSONString];
NSLog(@"json string is %@",updateString);
[request setHTTPBodyWithString:updateString];
[request setHTTPMethod:@"POST"];
//Call OAuth method to post status and call bak methods
OADataFetcher *fetcher = [[OADataFetcher alloc] init];
[fetcher fetchDataWithRequest:request
delegate:self
didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
didFailSelector:@selector(postUpdateApiCallResult:didFail:)];
}
3.) Get Recent Status:
On successful post,the next thing you need to do is to call the network update for getting recent post.For this you need to make request to networkApi somewhat like :
- (void)networkApiCall
{
NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/network/updates?scope=self&count=1&type=SHAR"];
OAMutableURLRequest *request =
[[OAMutableURLRequest alloc] initWithURL:url
consumer:self.linkedInLoginViewController.consumer
token:self.linkedInLoginViewController.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:)];
}
Based on network Api response ,data will be fetched.On Success,you need to code like this this to get current post :
- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSDictionary *person = [[[[[responseBody objectFromJSONString]
objectForKey:@"values"]
objectAtIndex:0]
objectForKey:@"updateContent"]
objectForKey:@"person"];
if ( [person objectForKey:@"currentShare"] )
{
self.status.text = [[person objectForKey:@"currentShare" ]
objectForKey:@"comment"];
}
}
This are some operations to play with linked In. You can try some others also.
Happy Coding….!!!
this was really helpful, but i got stuck at the posting part. the OA library and the jsonkit and everything under #2, i didn't quite understand. Are you able to expand on that? thats the one section that seems to assume some other things. I see OA is pretty old, is that still the recommended library?
ReplyDeleteany thoughts? I'm still struggling with oaconsumer
ReplyDelete