gpt4 book ai didi

ios - 如何在 iOS 7 中使用 LinkedIn Share Api

转载 作者:行者123 更新时间:2023-11-28 18:10:25 27 4
gpt4 key购买 nike

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

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

最佳答案

取回您的访问 token 后,您可以使用 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"];
}

关于ios - 如何在 iOS 7 中使用 LinkedIn Share Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24518269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com