IOSLinkedInAPIFix 2.0.4

IOSLinkedInAPIFix 2.0.4

测试已测试
语言语言 Obj-CObjective C
许可协议 MIT
发布最新发布2016年7月

李健棠维护。



  • 雅各布·冯·艾本、埃德华多·丰塞卡和李建棠

IOSLinkedInAPI

注意:截至2015年5月12日,LinkedIn对非合作伙伴的API使用施加了限制:请参阅https://developer.linkedin.com/blog/posts/2015/developer-program-changes


这是一个小巧、不会打扰到用户的库,可以方便地使用OAuth2在LinkedIn上进行身份验证和授权。API使用UIWebView对LinkedIn进行身份验证。

Authentication

Authentication

如果终端用户进行了身份验证,您将获得一个accesstoken,这是从LinkedIn获取数据所必需的。

为什么是这个库?

为什么还需要另一个LinkedIn库?尽管已经存在几个iOS库,它们都是围绕LinkedIn API进行包装的,但据我所知,它们没有一个使用OAuth2,而LinkedIn首选该协议。

如何开始使用

可以从cocoapods将库作为Pod获取。

如果您不使用CocoaPods,您始终可以从文件夹IOSLinkedInAPI中下载库并将文件导入到现有项目中。

示例代码

使用LIALinkedInApplication创建一个LinkedIn客户端。LIALinkedInApplication定义了有权访问用户LinkedIn数据的应用程序。

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:必须是http或https URL(LinkedIn所需),除此之外,终端点不需要做出任何响应。该库仅使用终端点来了解何时在UIWebView中截获调用。
  • clientId:在注册应用程序时LinkedIn提供的一个id。
  • clientSecret:在注册应用程序时LinkedIn提供的一个secret。
  • state:用于防止跨站请求伪造的状态。应该是难以猜测的东西。
  • grantedAccess:一个数组,告诉应用程序希望由最终用户授予哪些访问权限。请查阅完整的列表:http://developer.linkedin.com/documents/authentication
  • presentingViewController:UIWebView将从哪个视图控制器中模态展示。传递nil假设是根视图控制器。

之后,可以使用客户端检索accesstoken并通过LinkedIn API访问数据。

- (IBAction)didTapConnectWithLinkedIn:(id)sender {
  [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);
  }];
}

- (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);
  }];
}

代码示例从应用中检索访问令牌,并使用它获取授予访问权限的用户的数据。如果在UIWebView中点击取消按钮,则会执行取消回调,表示用户主动拒绝授权(参见图上说明)。如果上述步骤中的任意一步失败,则会执行失败回调。

示例应用

可以在此处找到一个小的示例应用程序: https://github.com/jeyben/IOSLinkedInAPI/tree/master/Example/IOSLinkedInAPI-Podexample/IOSLinkedInAPI-Podexample

该应用程序使用 Cocoapods。在克隆后,只需在目录中运行 'pods install',然后您就应该能够运行该应用程序了。

下一步

库当前负责处理身份验证和授权。我想改进库,使其更容易执行实际的 API 调用。我的当前想法是让客户端在获取访问令牌后将 accessToken 记录下来,并在随后的调用中自动将其与 format=json GET 参数一起追加。

如果您有关于如何实现的任何想法,请告诉我。

http://www.ancientprogramming.com