IOSLinkedInAPI:无法使用 LinkedIn API 共享帖子

2024-03-23

我一直在使用以下 SDK 将 LinkedIn 集成到 iOS 中并从 iDevices 共享帖子。

SDK 可在此处获取:https://github.com/jeyben/IOSLinkedInAPI https://github.com/jeyben/IOSLinkedInAPI

在这段代码中,我找不到正确的示例代码,但是我编写了一些可以共享帖子的代码。这是我的代码:

在代码中,我只有一个视图控制器,其中只使用了两个按钮,1)链接帐户[此按钮用于显示登录控制器并让用户成功登录到帐户]2)共享[允许用户在代表请求失败的登录用户]

视图控制器.h

#import <UIKit/UIKit.h>
#import "LIALinkedInApplication.h"
#import "LIALinkedInHttpClient.h"

@interface ViewController : UIViewController

@property (nonatomic, strong) LIALinkedInHttpClient *client;

- (IBAction) linkedInClicked:(id)sender;
- (void)requestMeWithToken:(NSString *)accessToken;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    LIALinkedInApplication *application = [LIALinkedInApplication applicationWithRedirectURL:@"http://www.google.com" clientId:@"w57zqiw6cv73" clientSecret:@"Pj5MVxtkpbefau1v" state:@"something" grantedAccess:@[@"r_fullprofile", @"r_network", @"rw_nus"]];
    self.client = [LIALinkedInHttpClient clientForApplication:application presentingViewController:nil];
    
}

- (IBAction) linkedInClicked:(id)sender { // Login into the account
    [self.client getAuthorizationCode:^(NSString *code) {
        [self.client getAccessToken:code success:^(NSDictionary *accessTokenData) {
            NSString *accessToken = [accessTokenData objectForKey:@"access_token"];
            [self requestMeWithToken:accessToken];
        }                   failure:^(NSError *error) {
            NSLog(@"Querying accessToken failed %@", error);
        }];
    }                      cancel:^{
        NSLog(@"Authorization was cancelled by user");
    }                     failure:^(NSError *error) {
        NSLog(@"Authorization failed %@", error);
    }];
}

- (IBAction) postMessage :(id)sender { // Post via logged in account, so, first go login and then share content
    NSString *strURL = @"https://api.linkedin.com/v1/people/~/shares";
    
    NSMutableDictionary *contents=[[NSMutableDictionary alloc] init];
    [contents setValue:@"description goes here" forKey:@"description"];
    [contents setValue:@"www.google.com" forKey:@"submitted-url"];
    [contents setValue:@"title goes here" forKey:@"title"];
    
    NSMutableDictionary *visible=[[NSMutableDictionary alloc] init];
    [visible setValue:@"anyone" forKey:@"code"];
    
    NSMutableDictionary *updatedic=[[NSMutableDictionary alloc] init];
    
    [updatedic setObject:visible forKey:@"visibility"];
    [updatedic setObject:contents forKey:@"content"];
    [updatedic setValue:@"Check out the LinkedIn Share API!" forKey:@"comment"];
    //[updatedic setValue:@"json" forKey: @"x-li-format"];
    
    [self.client POST:strURL parameters:updatedic success:^(AFHTTPRequestOperation *operation, NSDictionary *dict) {
        NSLog(@"Successfully posted", nil);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Failed post", nil);
    }];
}

- (void)requestMeWithToken:(NSString *)accessToken {
    [self.client GET:[NSString stringWithFormat:@"https://api.linkedin.com/v1/people/~?oauth2_access_token=%@&format=json", accessToken] parameters:nil success:^(AFHTTPRequestOperation *operation, NSDictionary *result) {
        NSLog(@"current user %@", result);
    }        failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failed to fetch current user %@", error);
    }];
}

要使此应用程序正常工作,请从上述 SDK 下载内容并将每个所需的文件添加到项目中。

当我尝试登录应用程序时,我收到成功消息,但之后当我尝试分享上述代码中所述的任何帖子时,我会失败并查看控制台是什么:

Printing description of error:
Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo=0x8a6d500 {NSErrorFailingURLKey=https://api.linkedin.com/v1/people/~/shares, NSLocalizedDescription=Request failed: unauthorized (401), NSUnderlyingError=0x8ab1bd0 "Request failed: unacceptable content-type: text/xml", AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x8a1f5f0> { URL: https://api.linkedin.com/v1/people/~/shares } { status code: 401, headers {
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Type" = "text/xml;charset=UTF-8";
    Date = "Tue, 20 May 2014 09:38:01 GMT";
    Server = "Apache-Coyote/1.1";
    "Transfer-Encoding" = Identity;
    Vary = "*";
    "Www-Authenticate" = "OAuth realm=\"https://api.linkedin.com\"";
    "X-LI-UUID" = "wUQ+CTiK5WDItDrWLbZJFQ==";
    "X-Li-Fabric" = "PROD-ELA4";
    "X-Li-Pop" = "PROD-ELA4";
    "x-li-format" = xml;
    "x-li-request-id" = 30K08X3IL7;
} }}

我尝试在 AFNetworking、LinkedIn 授权、未经授权的访问等方面进行大量搜索,但找不到任何相关内容。如果你们中有人知道这件事,或者建议我使用 LinkedIn iPhone SDK 的任何其他选项,请告诉我。


您需要将请求序列化器更改为AFJSONRequestSerializer并将字典上的键替换为驼峰式大小写。这是我的分享帖子代码:

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,字典的键采用驼峰式大小写。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOSLinkedInAPI:无法使用 LinkedIn API 共享帖子 的相关文章

  • AF网络。检查所有操作队列的下载进度

    我有一个使用 AFNetworking 制作的 iOS 应用程序 我有一个单例自定义 httpclient 子类 我用它来使用我的 api 并从服务器下载二进制文件 我需要下载大约 100 个文件 迭代我的 url 数组并为每个 url 创
  • 无法在 iOS 上将 CIImage 保存到文件而不发生内存泄漏

    下面的代码片段保存了一个CIImage到磁盘使用UIImage void applicationWillResignActive UIApplication application NSString filename Test png UI
  • SKPhysicsJoint:接触和碰撞不起作用

    在 IOS7 1 上 使用 SpriteKit 我创建了两个简单的矩形精灵以及相应的物理体 我设置了接触和碰撞位掩码 所有工作都完全符合我的预期 检测到接触并且碰撞防止两个矩形重叠 但是 当我创建 SKPhysicsJointSpring
  • 向特定联系号码发送 Whatsapp 消息(Swift 项目)

    我正在尝试向存储在全局变量中的收件人号码发送 Whatsapp 消息 通过使用这个简单的代码 let whatsAppUrl NSURL string whatsapp globalPhone if UIApplication shared
  • 视图隐藏在 UINavigationBar iOS 7 下面

    早些时候 我的项目使用的是 iOS 6 1 最近我已经切换到 iOS 7 对于我知道的很多更改 我更新了我的代码 但是我观察到了一个奇怪的行为 我在每个屏幕上的视图都隐藏在导航栏下方 重新定位视图解决了 iOS7 的问题 但为旧版 iOS
  • 从服务器下载图像以显示在 CollectionView 上

    我正在开发一个用户可以出售 购买的产品应用程序 该应用程序基于集合视图 集合视图具有集合单元格 其中显示产品图像缩略图 以下代码从服务器获取产品图像 并等待下载所有图像 然后将它们显示在单元格中 以下代码有效 但用户需要等待 10 20 秒
  • 使用Sprite Kit如何启用iAd?

    在IOS 7中 我们可以非常轻松地启用iAd 使用 self canDisplayBannerAds YES in code 查看是否加载UIViewController的 但是 我无法在我的 ViewController 加载 SKSce
  • 如何将 AFHTTPClient、Afnetworking 1.0 迁移到 2.0

    我的问题是我有一个旧代码 我不知道如何更改它 我有 1 个名为 API HTTPClient 的类 我对 2 个方法有问题 因为我不知道如何将它们放入 2 0 中 这 void commandWithParams NSMutableDict
  • 如何修复 Chrome 开发者窗口中的“待处理”状态?

    When I try to include social media scripts into my page I get the pending status in Chrome on some computers not all of
  • 当用户点击 iPhone 屏幕上的任意位置时,iOS7 中的 UIActionSheet 会消失

    我注意到在iOS 7 a UIActionSheet automatically dismisses when a user taps anywhere on the screen on an iPhone iOS 6 中并非如此 并且会导
  • ios7 UITableViewCell SelectionStyle 不会变回蓝色

    Xcode 5 0 iOS 7 并更新现有应用程序 UITableView所选行现在是灰色的 而不是蓝色的 据我所知 他们更改了默认值selectionStyle至灰色 但 蓝色 仍然是 IB 的一个选项UITableViewCellSel
  • ios7 中的自动续订订阅

    我知道这个问题已经被问过很多次了 但没有一个问题对我有帮助 而且我不确定这些答案中哪些在 iOS 7 中仍然有效 我正在做一些带有自动续订订阅的应用程序 我已经成功创建了我的产品 我可以与测试用户一起购买它 我的问题是 检查特定用户是否仍然
  • IOS 7 UITextField resignFirstResponder 坏

    当我在自定义单元格内使用 UItextField 时 以及当我 resignFirstResponder 文本字段时 我发生崩溃 但它不再可见 表视图滚动到窗口外 我仍然可以找到文本字段 指针继续可访问 它不为空 并且崩溃仅发生在 IOS7
  • UITableView 顶部出现间隙[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 我不确定现在问这个问题是否合适 我正在表视图上使用 Xcode 5 预览版 现在的问题是我的表格视图是否被选择为group比我在第一个单元
  • 如何执行一批相互依赖的 AFNetworking 请求

    我需要执行一系列按顺序运行的服务器调用 并且只有在所有先前的请求都成功的情况下才能执行一个请求 所以 我的想法是创建一个AFHTTPRequestOperation对于每个请求和使用 myAFHTTPClient enqueueBatchO
  • AVSpeechUtterance 最大音量非常安静且速率非常快

    我正在考虑向我的应用程序添加语音提示 并在 iOS 7 中测试 AVSpeechUtterance 但默认语音速率非常快 最低语速更容易理解 但是最大音量值1太安静了 我在 iPhone 4 上进行了测试 并将音量调到最大 一定是出了什么问
  • AFNetworking 无法编译

    我完全无法尝试使用 AFNetworking 在模拟器上运行项目 我之前在其他项目中使用过这种依赖关系 所以我不明白这里出了什么问题 首先 我尝试运行项目时出现错误 Undefined symbols for architecture x8
  • 如何将故事板连接到视图控制器

    在新的 xcode 5 中 如何在界面生成器中使用下拉菜单将故事板视图控制器连接到类 现在这是如何完成的 除非我误解了你的要求 否则它仍然存在 以下是将自定义视图控制器类分配给视图控制器的步骤 在左侧场景列表中选择您的视图控制器 选择右侧的
  • UIButton 未出现在 iPhone 5S 上

    总的来说 我对 iOS 开发和开发还很陌生 我一直在开发时间 记录保存应用程序 但遇到了一个奇怪的问题 在我的一个视图控制器上 我有一个 UITableView 每个单元格都是一个按钮 可通往不同的视图控制器 在第一个单元格上 用户应该能够
  • UIToolbar setBackgroundColor 没有完全改变颜色

    我正在尝试设置 a 的背景颜色UIToolBar 我尝试从 IB 的属性检查器中选择颜色 并尝试通过编程方式设置它setBackgroundColor UIColor 两种解决方案都有效 但只是部分有效 颜色与白色混合了大约 50 并且工具

随机推荐