简体   繁体   English

如何在iOS 7中使用LinkedIn Share Api

[英]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. 我添加了使用oauth2在iOS 7应用程序中在LinkedIn上共享文章的功能。 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. 我知道我在这里发布: http : //api.linkedin.com/v1/people/~/shares附加了令牌。

But every example then just has the same code using OAMutableRequest, building the dictionary,etc. 但是,每个示例使用OAMutableRequest,构建字典等都具有相同的代码。 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. 这是公认的最佳实践吗,该库在3年内没有更新,因此在弧和其他方面存在错误。 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. 我似乎无法找到如何使用参数linkedin需要在网站上发布内容的方式构建发布请求。 Is OAMutableRequest the only way? OAMutableRequest是唯一的方法吗? If so, how have people updated it to work? 如果是这样,人们如何对其进行更新? Thanks so much! 非常感谢!

After retrieve your Access Token you can use AFNetworking for a POST request like this example code: 检索访问令牌后,可以将AFNetworking用于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. 重要提示:根据Linkedin API,字典的键为驼峰式。

In the case linkedin give bad request (error 400), another way to create the dictionary is: 在linkedin提出错误请求(错误400)的情况下,创建字典的另一种方法是:

    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"];
    }

Andr3a88's answer might work, but I could never get everything figured out with linkedin's side. Andr3a88的答案可能有用,但是我无法从linkedin的角度弄清所有问题。 Luckily, they finally released a full sdk: https://developer.linkedin.com/docs/share-on-linkedin , https://developer.linkedin.com/docs/ios-sdk 幸运的是,他们终于发布了完整的sdk: https : //developer.linkedin.com/docs/share-on-linkedin,https : //developer.linkedin.com/docs/ios-sdk

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM