我一直在使用以下SDK來執行LinkedIn到iOS的集成以及從iDevices共享文章。IOSLinkedInAPI:無法與LinkedIn API共享文章
SDK可以在這裏找到:https://github.com/jeyben/IOSLinkedInAPI
在這段代碼中,我找不到合適的示例代碼,但是,但是我已經寫了一些代碼,通過它後可以共享。這裏是我的代碼:
在代碼中,我只有一個viewcontroller,其中我只有兩個按鈕,1)Linked In Account [此按鈕用於呈現登錄控制器並獲取用戶成功登錄到帳戶] 2 )共享[允許用戶在請求失敗共享代表登錄的用戶內容]
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);
}];
}
爲了使這個應用程序的工作,從上面的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提供其他選項。
同樣的問題任何解決方案 – mychar
你有沒有想過這個? – user3344977
我已經停止了這個項目:( –