如何在iOS 7中使用LinkedIn Share Api

ski*_*atg 0 linkedin ios oauth-2.0

我正在添加使用oauth2在iOS 7应用程序中在LinkedIn上分享文章的功能.我已经通过身份验证并拥有访问令牌.文档似乎很清楚,但实际上发布的事情很奇怪.我知道我在这里发帖:http://api.linkedin.com/v1/people/~/shares附加令牌.

但是每个例子都使用OAMutableRequest,构建字典等相同的代码.但他们从不解释那是什么,如何整合那个图书馆或任何东西,它只是奇怪.这是公认的最佳实践,图书馆在3年内没有更新,因此它有弧形和其他东西的错误.所有代码示例都提到了相同的"消费者"属性,没有讨论如何或为什么需要这样做.我似乎无法找到如何使用参数linkedin构建post请求,以便在网站上发布内容.OAMutableRequest是唯一的方法吗?如果是这样,人们如何更新它的工作?非常感谢!

And*_*a88 7

检索您的访问令牌后,您可以使用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);
}];
Run Code Online (Sandbox Code Playgroud)

重要提示:根据Linkedin API,字典的键是camel case.

在facebook提供错误请求(错误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"];
    }
Run Code Online (Sandbox Code Playgroud)