我一直在使用以下 SDK 将 LinkedIn 集成到 iOS 中并从 iDevices 共享帖子。
SDK 可在此处获取:https://github.com/jeyben/IOSLinkedInAPI https://github.com/jeyben/IOSLinkedInAPI
在这段代码中,我找不到正确的示例代码,但是我编写了一些可以共享帖子的代码。这是我的代码:
在代码中,我只有一个视图控制器,其中只使用了两个按钮,1)链接帐户[此按钮用于显示登录控制器并让用户成功登录到帐户]2)共享[允许用户在代表请求失败的登录用户]
视图控制器.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
视图控制器.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(@"Querying 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);
}];
}
要使此应用程序正常工作,请从上述 SDK 下载内容并将每个所需的文件添加到项目中。
当我尝试登录应用程序时,我收到成功消息,但之后当我尝试分享上述代码中所述的任何帖子时,我会失败并查看控制台是什么:
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;
} }}
我尝试在 AFNetworking、LinkedIn 授权、未经授权的访问等方面进行大量搜索,但找不到任何相关内容。如果你们中有人知道这件事,或者建议我使用 LinkedIn iPhone SDK 的任何其他选项,请告诉我。