From ad2fbba904ec4d523edc3ca6c91ab0334fc9c0f7 Mon Sep 17 00:00:00 2001 From: Jacob von Eyben Date: Thu, 30 Jan 2014 19:55:38 +0100 Subject: [PATCH] Updated example and documentation - prepared for release of version 2.0.0 of the API. --- Example/IOSLinkedInAPI-Example/.gitignore | 1 + .../LIAViewController.m | 2 +- README.md | 55 ++++++++++++------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/Example/IOSLinkedInAPI-Example/.gitignore b/Example/IOSLinkedInAPI-Example/.gitignore index bb1a5dd..4bef3e7 100644 --- a/Example/IOSLinkedInAPI-Example/.gitignore +++ b/Example/IOSLinkedInAPI-Example/.gitignore @@ -1,2 +1,3 @@ Pods/ +.idea IOSLinkedInAPI-Example/LIALinkedInClientExampleCredentials.h diff --git a/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/LIAViewController.m b/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/LIAViewController.m index 5bf63a6..30612eb 100644 --- a/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/LIAViewController.m +++ b/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample/LIAViewController.m @@ -27,7 +27,7 @@ - (void)viewDidLoad { - (IBAction)didTapConnectWithLinkedIn:(id)sender { - [_client getAuthorizationCode:^(NSString *code) { + [self.client getAuthorizationCode:^(NSString *code) { [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) { NSString *accessToken = [accessTokenData objectForKey:@"access_token"]; [self requestMeWithToken:accessToken]; diff --git a/README.md b/README.md index 46c59af..c55e6d0 100644 --- a/README.md +++ b/README.md @@ -23,18 +23,18 @@ The library can be fetched as a Pod from [cocoapods](http://cocoapods.org/?q=ios If you aren't using Cocoapods you can always download the libary and import the files from the folder IOSLinkedInAPI into your existing project. -Example -------- +Example Code +------------ A LinkedIn client is created using a LIALinkedInApplication. A LIALinkedInApplication defines the application which is granted access to the users linkedin data. ``` objective-c -LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"http://www.ancientprogramming.com" - clientId:@"clientId" - clientSecret:@"clientSecret" - state:@"DCEEFWF45453sdffef424" - grantedAccess:@[@"r_fullprofile", @"r_network"]]; -LIALinkedInHttpClient *client = [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil]; +LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"http://www.ancientprogramming.com/liaexample" + clientId:@"clientId" + clientSecret:@"clientSecret" + state:@"DCEEFWF45453sdffef424" + grantedAccess:@[@"r_fullprofile", @"r_network"]]; +return [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil]; ``` * redirectURL: has to be a http or https url (required by LinkedIn), but other than that, the endpoint doesn't have to respond anything. The library only uses the endpoint to know when to intercept calls in the UIWebView. * clientId: The id which is provided by LinkedIn upon registering an application. @@ -45,27 +45,42 @@ LIALinkedInHttpClient *client = [LIALinkedInHttpClient clientForApplication:appl Afterwards the client can be used to retrieve an accesstoken and access the data using the LinkedIn API: ``` objective-c -client getAuthorizationCode:^(NSString * code) { +- (IBAction)didTapConnectWithLinkedIn:(id)sender { + [self.client getAuthorizationCode:^(NSString *code) { [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) { - NSString *accessToken = [accessTokenData objectForKey:@"access_token"]; - [self.client getPath:[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); - }]; - } failure:^(NSError *error) { - NSLog(@"Quering accessToken failed %@", error); + NSString *accessToken = [accessTokenData objectForKey:@"access_token"]; + [self requestMeWithToken:accessToken]; + } failure:^(NSError *error) { + NSLog(@"Quering accessToken failed %@", error); }]; -} cancel:^{ + } cancel:^{ NSLog(@"Authorization was cancelled by user"); -} failure:^(NSError *error) { + } failure:^(NSError *error) { NSLog(@"Authorization failed %@", error); -}]; + }]; +} + +- (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); + }]; +} ``` The code example retrieves an access token and uses it to get userdata for the user which granted the access. The cancel callback is executed in case the user actively declines the authorization by pressing cancel button in the UIWebView (see illustration above). The failure callbacks is executed in case either the of the steps fails for some reason. +Example App +------------ +A small example application can be found here: +https://github.com/jeyben/IOSLinkedInAPI/tree/master/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample + +It uses the cocoapods. +Just run 'pods install' in the directory after your clone and you should be able to run the app afterwards + + Next step -------------------- The library is currently handling the authentication and authorization.