无法使用OAuth Starter Kit for LinkedIn进行分享

时间:2012-10-09 13:29:51

标签: ios oauth linkedin

我使用OAuthStarter Kit访问LinkedIn API。我成功获得了访问权限以获取权限。

  

rw_ns
     的 r_basicprofile
     的 r_fullprofile

可以获取个人资料详细信息以及仅共享评论和共享网址或图片或说明等...我使用以下代码:

-(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
}

我不知道问题是什么。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:3)

我修好了。 问题出在后期参数

  1. 根据api中链接的文档,如果我们在请求正文中使用JSON 那么post params的键必须是 camel case ,而xml “ - ”使用分隔的键名
  2. 分享参数,例如 submittedUrl,subsubmittedImageUrl,description,title **等。必须是名为** content 的键值。
  3. 所以我修改了代码如下..

    -(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:)];
    
    }
    

    现在代码对我有用..