如何在iOS 7中使用LinkedIn共享Api
我正在添加使用oauth2在iOS 7应用程序中共享LinkedIn文章的功能。我已通过身份验证并拥有访问令牌。文档似乎很清楚这一点,但奇怪的是,事实上,事情变得很模糊。我知道我在这里张贴:附加令牌如何在iOS 7中使用LinkedIn共享Api,ios,oauth-2.0,linkedin,Ios,Oauth 2.0,Linkedin,我正在添加使用oauth2在iOS 7应用程序中共享LinkedIn文章的功能。我已通过身份验证并拥有访问令牌。文档似乎很清楚这一点,但奇怪的是,事实上,事情变得很模糊。我知道我在这里张贴:附加令牌 但是每个例子都有相同的代码,使用OAMutableRequest,构建字典,等等。但是他们从来没有解释这是什么,如何合并这个库或者其他任何东西,这很奇怪。这是公认的最佳实践吗?图书馆已经三年没有更新了,所以它在arc和其他方面存在错误。所有代码示例都提到了相同的消费者属性,但没有讨论如何或为什么需要
但是每个例子都有相同的代码,使用OAMutableRequest,构建字典,等等。但是他们从来没有解释这是什么,如何合并这个库或者其他任何东西,这很奇怪。这是公认的最佳实践吗?图书馆已经三年没有更新了,所以它在arc和其他方面存在错误。所有代码示例都提到了相同的消费者属性,但没有讨论如何或为什么需要这样做。我似乎找不到如何使用linkedin在网站上发布内容所需的参数来构建post请求。OAMutableRequest是唯一的方法吗?如果是这样,人们是如何更新它以使其工作的?非常感谢 检索访问令牌后,您可以将其用于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"];
}
Andr3a88的答案可能有用,但我永远也无法从linkedin的角度解决所有问题。幸运的是,他们最终发布了完整的sdk:,您好,我已经按照您所说的实现了,但是我收到了400个错误,您能帮我解决请求失败的问题吗:错误的请求400通常这个错误是由于api调用参数所用的字典配置错误而发生的。尝试只设置注释键和可见性键。然后空响应只设置注释键和可见性键,但我们希望共享文本和链接。@08442我不明白linkedin为什么总是给出这些错误,我用另一种方法更新了答案以构建字典。在我的实现中,我的工作效率为100%。即使只设置注释和可见性键,我也有同样的错误请求问题。有什么想法吗?