【问题标题】:Can't share using OAuth Starter Kit for LinkedIn无法使用适用于 LinkedIn 的 OAuth Starter Kit 共享
【发布时间】:2012-09-29 20:27:42
【问题描述】:

我使用 OAuthStarter Kit 来访问 LinkedIn API。我成功获得了权限的访问令牌。

rw_ns
r_basicprofile
r_fullprofile

可以获取配置文件详细信息以及仅共享 cmets 以及共享 URL 或图像或描述等...我使用以下代码:

-(void)postUpdate
 {
   NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
   OAMutableURLRequest *request =
   [[OAMutableURLRequest alloc] initWithURL:url
                            consumer:[self getConsumer]
                               token:self.accesstoken
                            callback:nil
                   signatureProvider:nil];

   NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:

                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                     @"anyone",@"code",nil], @"visibility",
                    @"title goes here",@"title",
                    @"comment goes here", @"comment",
                    @"description goes here",@"description",
                     @"www.google.com",@"submitted-url",
                    @"http://economy.blog.ocregister.com/files/2009/01/linkedin-logo.jpg",@"submitted-image-url",
                    nil];
   [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
   [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

   NSString *updateString = [update JSONString];
   [request setHTTPBodyWithString:updateString];
    [request setHTTPMethod:@"POST"];

   OADataFetcher *fetcher = [[OADataFetcher alloc] init];
   [fetcher fetchDataWithRequest:request
                 delegate:self
        didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
          didFailSelector:@selector(postUpdateApiCallResult:didFail:)];

  }

但我收到如下响应错误:

 2012-10-09 18:27:29.906 SocialConnectTest[9460:19a03] data {
 "errorCode": 0,
 "message": "Invalid xml {Expected elements 'post-network-update@http://api.linkedin.com/v1 id@http://api.linkedin.com/v1 visibility@http://api.linkedin.com/v1 comment@http://api.linkedin.com/v1 attribution@http://api.linkedin.com/v1 content@http://api.linkedin.com/v1 private-message@http://api.linkedin.com/v1 share-target-reach@http://api.linkedin.com/v1' instead of 'submitted-url@http://api.linkedin.com/v1' here in element share@http://api.linkedin.com/v1}",
 "requestId": "W2G7WJDHOJ",
 "status": 400,
 "timestamp": 1349787449685
}

我不知道问题是什么。谁能帮我解决这个问题?

【问题讨论】:

  • 看起来端点需要 XML,即使您已指定 JSON。有没有试过推送 XML 看看报错是不是一样?

标签: ios oauth linkedin


【解决方案1】:

我修好了。 问题出在帖子参数中

  1. 根据 Linked in api 的文档,如果我们在请求正文中使用 JSON 那么 post 参数的键必须是 驼峰式,而在 xml 中 "-" 使用单独的键名
  2. submittedUrl、subsubmittedImageUrl、description、title **等共享参数。必须是名为 **content 的键的值。

所以我修改了代码如下..

-(void)postUpdateHERE
{
  NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/shares"];
  OAMutableURLRequest *request =
  [[OAMutableURLRequest alloc] initWithURL:url
                            consumer:[self getConsumer]
                               token:self.accesstoken
                            callback:nil
                   signatureProvider:nil];

  NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:

                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                     @"anyone",@"code",nil], @"visibility",

                    @"comment goes here", @"comment",
                    [[NSDictionary alloc]
                     initWithObjectsAndKeys:
                    @"description goes here",@"description",
                    @"www.google.com",@"submittedUrl",
                      @"title goes here",@"title",
                    @"http://economy.blog.ocregister.com/files/2009/01/linkedin-logo.jpg",@"submittedImageUrl",nil],@"content",
                    nil];
  [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];
  [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  NSString *updateString = [update JSONString];
  [request setHTTPBodyWithString:updateString];
  [request setHTTPMethod:@"POST"];
  OADataFetcher *fetcher = [[OADataFetcher alloc] init];
  [fetcher fetchDataWithRequest:request
                 delegate:self
        didFinishSelector:@selector(postUpdateApiCallResult:didFinish:)
          didFailSelector:@selector(postUpdateApiCallResult:didFail:)];

}

现在代码对我有用..

【讨论】:

  • 我尝试了您的代码,但它在 OAuth Starter Kit 项目中崩溃。你从哪里叫你postUpdateHERE函数?
  • 这只是一个可以在事件发生时调用的函数,您可以记录并查看导致崩溃的原因
  • 你能帮我解决这个问题吗? stackoverflow.com/questions/20141897/…
  • 是否调用了 didfinisslector 或 didfailselector ?在请求之后?
  • 你也可以评论我的问题吗? stackoverflow.com/questions/23758142/…
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2017-11-22
  • 2012-08-29
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
相关资源
最近更新 更多