skinsfan00atg
skinsfan00atg

Reputation: 571

How to use LinkedIn Share Api in iOS 7

I'm adding the ability to share an article on LinkedIn in an iOS 7 app using oauth2. I've gotten thru the authentication and have the access token. The documentation seems to be pretty clear about that, but it's odd, to actually post, things get pretty vague. I know I post here: http://api.linkedin.com/v1/people/~/shares appending the token.

But every example then just has the same code using OAMutableRequest, building the dictionary,etc. but they never explain what that is, how to incorporate that library or anything, its just strange. Is this the accepted best practice, the library hasn't been updated in 3 years so it has errors for arc and other things. All the code examples mention the same "consumer" property with no discussion of how or why that's needed. I can't seem to find how you build the post request with the parameters linkedin needs to post something on the site. Is OAMutableRequest the only way? If so, how have people updated it to work? Thanks so much!

Upvotes: 0

Views: 1669

Answers (2)

skinsfan00atg
skinsfan00atg

Reputation: 571

Andr3a88's answer might work, but I could never get everything figured out with linkedin's side. Luckily, they finally released a full sdk: https://developer.linkedin.com/docs/share-on-linkedin, https://developer.linkedin.com/docs/ios-sdk

Upvotes: 0

Andr3a88
Andr3a88

Reputation: 689

After retrieve your Access Token you can use AFNetworking for a POST request like this example code:

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.

In the case linkedin give bad request (error 400), another way to create the dictionary is:

    NSMutableDictionary *update = [[NSMutableDictionary alloc] init];
    if(message)
    {
        //Set visibility
        NSDictionary *visibility = [[NSDictionary alloc] initWithObjectsAndKeys:@"anyone", @"code", nil];
        [update setObject:visibility forKey:@"visibility"];

        //Set comment
        [update setObject:message forKey:@"comment"];

        //Set content or append imageUrl/postUrl to message to share
        NSMutableDictionary *content = [[NSMutableDictionary alloc] init];

        if(postUrl)
            [content setObject:imageUrl forKey:@"submittedUrl"];

        if(imageUrl)
            [content setObject:imageUrl forKey:@"submittedImageUrl"];

        if(postUrl || imageUrl)
            [update setObject:content forKey:@"content"];
    }

Upvotes: 7

Related Questions