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