IOSLinkedInAPI: Can't share post with the LinkedIn API

萝らか妹 提交于 2019-12-04 12:36:56

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:

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.

https://github.com/pmilanez/MIS-Linkedin-Share

Use this library to share on linkedIn, It's pretty easy.

First , store the access token when you logged in through IOSLinkdinAPI.

for example . if i have singleton class DataObjects and having property name 'accessTokesForLinkedIn' then change in this method will be

- (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 :-

-(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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!