【问题标题】:How to use LinkedIn Share Api in iOS 7如何在 iOS 7 中使用 LinkedIn Share Api
【发布时间】:2015-07-17 04:24:24
【问题描述】:

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

但是每个示例都只有使用 OAMutableRequest、构建字典等的相同代码。但他们从不解释那是什么,如何合并那个库或任何东西,这很奇怪。这是公认的最佳实践吗,该库已 3 年未更新,因此它在 arc 和其他方面存在错误。所有代码示例都提到了相同的“消费者”属性,没有讨论如何或为什么需要它。我似乎无法找到您如何使用linkedin 需要在网站上发布内容的参数构建发布请求。 OAMutableRequest 是唯一的方法吗?如果是这样,人们如何对其进行更新以使其正常工作?非常感谢!

【问题讨论】:

    标签: ios oauth-2.0 linkedin


    【解决方案1】:

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

    重要提示:根据Linkedin API,字典的键是驼峰式的。

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

    【讨论】:

    • 嗨,我已经按照你说的那样实现了,但是我收到 400 错误,你能帮我解决吗?请求失败:错误请求 (400)
    • 这个错误通常是由于用于 api 调用参数的字典配置错误造成的。尝试只设置 "comment""visibility" 键。
    • 然后出现空响应,仅设置“评论”和“可见性”键,但我们想共享文本和链接。
    • @08442 我不明白为什么linkedin 总是给出这些错误,我用另一种构建字典的方法更新了答案。在我的实施中以 100% 的速度运行。
    • 我遇到了同样的“错误请求”问题,即使只设置“评论”和“可见性”键也是如此。有什么想法吗?
    【解决方案2】:

    Andr3a88 的答案可能有效,但我永远无法在linkedin 方面弄清楚所有事情。幸运的是,他们终于发布了完整的 sdk:https://developer.linkedin.com/docs/share-on-linkedinhttps://developer.linkedin.com/docs/ios-sdk

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-25
      • 1970-01-01
      • 2017-03-16
      • 2020-05-01
      • 1970-01-01
      相关资源
      最近更新 更多