I have been using following SDK to perform integration of LinkedIn into iOS and sharing post from iDevices.
我一直在使用以下SDK来执行LinkedIn与iOS的集成以及从iDevices分享帖子。
SDK is available here: https://github.com/jeyben/IOSLinkedInAPI
SDK可在此处获得:https://github.com/jeyben/IOSLinkedInAPI
In this code I can't find proper example code but however I had written some code through which post can be shared. Here is my code:
在这段代码中,我找不到合适的示例代码,但是我已经编写了一些可以共享帖子的代码。这是我的代码:
In the code I have only single viewcontroller in which I have taken only two buttons, 1) Linked In Account [This button is used to present login controller and gets user successfully logged into the account] 2) Share [Allows user to share content on behalf of logged in user where the request fails]
在代码中,我只有一个viewcontroller,其中我只有两个按钮,1)Linked In Account [此按钮用于显示登录控制器并让用户成功登录帐户] 2)分享[允许用户共享内容代表请求失败的登录用户]
ViewController.h
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
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);
}];
}
To make this app work, download content from the above SDK and add every required file into the project.
要使此应用程序正常运行,请从上面的SDK下载内容,并将所有必需的文件添加到项目中。
When I try to login the app, I get successful message but after that when I try to share any post as described in above code, I get failure and see what is console:
当我尝试登录应用程序时,我收到了成功的消息,但在此之后,当我尝试按照上面的代码分享任何帖子时,我会失败并看到什么是控制台:
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;
} }}
I have tried searching a lot on AFNetworking, LinkedIn authorization, Unauthorized access, etc. but could not found anything over this. Let me know if anyone of you are aware of something about this or suggest me any other option for LinkedIn iPhone SDK.
我已经尝试过很多关于AFNetworking,LinkedIn授权,未经授权的访问等等,但是在这方面找不到任何东西。如果你们中的任何人都知道这件事或者建议我使用LinkedIn iPhone SDK的任何其他选项,请告诉我。
3 个解决方案
#1
2
You need to change the request serializer to AFJSONRequestSerializer and replace the keys on the dictionary in camel case. This is my code for share post:
您需要将请求序列化程序更改为AFJSONRequestSerializer并以驼峰形式替换字典上的键。这是我的分享帖子代码:
NSString *stringRequest = @"https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=ACCESS_TOKEN&format=json";
//Request parameter on a dictionary (keys in camel case)
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc] initWithObjectsAndKeys: @"anyone",@"code",nil], @"visibility",
@"comment to share", @"comment",
[[NSDictionary alloc] initWithObjectsAndKeys:@"description share", @"description",
@"link_url", @"submittedUrl",
@"title share",@"title",
@"image_url",@"submittedImageUrl",nil],
@"content",nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;
[manager POST:stringRequest parameters:update success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"result: %@", responseObject);
completionBlock(YES, responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError([error localizedDescription]);
completionBlock(NO, nil, error);
}];
Important: the keys of the dictionary are in camel case according to Linkedin API.
重要提示:根据Linkedin API,字典的键是camel case。
#2
0
https://github.com/pmilanez/MIS-Linkedin-Share
https://github.com/pmilanez/MIS-Linkedin-Share
Use this library to share on linkedIn, It's pretty easy.
使用这个库在linkedIn上分享,这很简单。
#3
0
First , store the access token when you logged in through IOSLinkdinAPI.
首先,在通过IOSLinkdinAPI登录时存储访问令牌。
for example . if i have singleton class DataObjects and having property name 'accessTokesForLinkedIn' then change in this method will be
例如 。如果我有单例类DataObjects并且具有属性名称'accessTokesForLinkedIn',则此方法中的更改将是
- (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"];
[[DataObjects sharedDataObjects] setAccessTokenForLinkedIn:accessToken];
[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);
}];
}
and now when we have to share any text or URL then the method is :-
现在,当我们必须共享任何文本或URL时,方法是: -
-(void)sharetoLinkedIn:(NSString *)title desc:(NSString *)description path:(NSString *)submitted_url imagePath:(NSString *)submitted_image_url postString:(NSString *)comment
{
NSString *stringRequest = [NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=%@&format=json",[[DataObjects sharedDataObjects] accessTokenForLinkedIn]] ;
NSDictionary *param = @{
@"content":@{
@"title":title,
@"description":description,
@"submitted-url":submitted_url,
@"submitted-image-url":submitted_image_url
},
@"comment": comment,
@"visibility": @{
@"code": @"anyone"
}
};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringRequest]];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:param options:0 error:nil]];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:@{@"Content-Type":@"application/json",
@"x-li-format":@"json"
}];
AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"result: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([operation.responseString class] == [NSDictionary class]) {
//[Utility showAlert:@"LinkedIn" mess:[(NSDictionary *)operation.responseString objectForKey:@"message"]];
NSLog(@"error: %@", [(NSDictionary *)operation.responseString objectForKey:@"message"]);
}
NSLog(@"error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
}
#1
2
You need to change the request serializer to AFJSONRequestSerializer and replace the keys on the dictionary in camel case. This is my code for share post:
您需要将请求序列化程序更改为AFJSONRequestSerializer并以驼峰形式替换字典上的键。这是我的分享帖子代码:
NSString *stringRequest = @"https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=ACCESS_TOKEN&format=json";
//Request parameter on a dictionary (keys in camel case)
NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
[[NSDictionary alloc] initWithObjectsAndKeys: @"anyone",@"code",nil], @"visibility",
@"comment to share", @"comment",
[[NSDictionary alloc] initWithObjectsAndKeys:@"description share", @"description",
@"link_url", @"submittedUrl",
@"title share",@"title",
@"image_url",@"submittedImageUrl",nil],
@"content",nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
manager.requestSerializer = requestSerializer;
[manager POST:stringRequest parameters:update success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"result: %@", responseObject);
completionBlock(YES, responseObject, nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
DDLogError([error localizedDescription]);
completionBlock(NO, nil, error);
}];
Important: the keys of the dictionary are in camel case according to Linkedin API.
重要提示:根据Linkedin API,字典的键是camel case。
#2
0
https://github.com/pmilanez/MIS-Linkedin-Share
https://github.com/pmilanez/MIS-Linkedin-Share
Use this library to share on linkedIn, It's pretty easy.
使用这个库在linkedIn上分享,这很简单。
#3
0
First , store the access token when you logged in through IOSLinkdinAPI.
首先,在通过IOSLinkdinAPI登录时存储访问令牌。
for example . if i have singleton class DataObjects and having property name 'accessTokesForLinkedIn' then change in this method will be
例如 。如果我有单例类DataObjects并且具有属性名称'accessTokesForLinkedIn',则此方法中的更改将是
- (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"];
[[DataObjects sharedDataObjects] setAccessTokenForLinkedIn:accessToken];
[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);
}];
}
and now when we have to share any text or URL then the method is :-
现在,当我们必须共享任何文本或URL时,方法是: -
-(void)sharetoLinkedIn:(NSString *)title desc:(NSString *)description path:(NSString *)submitted_url imagePath:(NSString *)submitted_image_url postString:(NSString *)comment
{
NSString *stringRequest = [NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=%@&format=json",[[DataObjects sharedDataObjects] accessTokenForLinkedIn]] ;
NSDictionary *param = @{
@"content":@{
@"title":title,
@"description":description,
@"submitted-url":submitted_url,
@"submitted-image-url":submitted_image_url
},
@"comment": comment,
@"visibility": @{
@"code": @"anyone"
}
};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stringRequest]];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:param options:0 error:nil]];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:@{@"Content-Type":@"application/json",
@"x-li-format":@"json"
}];
AFHTTPRequestOperation *op = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"result: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if ([operation.responseString class] == [NSDictionary class]) {
//[Utility showAlert:@"LinkedIn" mess:[(NSDictionary *)operation.responseString objectForKey:@"message"]];
NSLog(@"error: %@", [(NSDictionary *)operation.responseString objectForKey:@"message"]);
}
NSLog(@"error: %@", error);
}];
[[NSOperationQueue mainQueue] addOperation:op];
}