2012-10-25 47 views
3

我正在使用OAuth-Sample-Client代码通过链接邀请API从电子邮件地址发送邀请。 我已经创建了与开发者网站中描述的相同的主体,但是现在我得到了401错误,我已经搜索了很多关于它的信息,但是没有得到任何解决方案让我从任何网站或论坛中获得它。 我的回复如下。LinkedIn Invitation API错误401 [未授权] -iPhone sdk

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<error> 
    <status>401</status> 
    <timestamp>1351159386437</timestamp> 
    <request-id>YX4NRU3S7J</request-id> 
    <error-code>0</error-code> 
    <message>[unauthorized]. OAU:ron8e8nko3te|e0a96317-dfdc-44fb-b427-5655e02f97ed|*01|*01:1351159294:dRCDN6b3K9F/mwWAaByKZQPgeVw=</message> 
</error> 

这里我JSON字符串会是这样的:

{ “体”: “说是的!”, “主题”: “邀请 连接”, “收件人”:{ “值”:[{ “人”:{ “第一名”: “测试”, “最后的名称”: “测试”, “_路径”: “/ people/[email protected]”}}] },“item-content”:{“invitation-request”:{“connect-type”:“friend”}}}

我已经使用下面的代码发送邀请。

- (IBAction)postButton_TouchUp:(UIButton *)sender 
{  
    [statusTextView resignFirstResponder]; 
    NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/mailbox"]; 
    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url 
            consumer:oAuthLoginView.consumer 
             token:oAuthLoginView.accessToken 
            callback:nil 
          signatureProvider:[[OAHMAC_SHA1SignatureProvider alloc] init]]; 


    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 

    NSDictionary *temp=[[NSDictionary alloc]initWithObjectsAndKeys:@"/people/[email protected]",@"_path",@"test",@"first-name",@"test",@"last-name", nil]; 
    NSDictionary *temp2=[[NSDictionary alloc] initWithObjectsAndKeys:temp,@"person",nil]; 
    NSArray *arr2=[[NSArray alloc]initWithObjects:temp2, nil]; 
    NSDictionary *value=[[NSDictionary alloc]initWithObjectsAndKeys:arr2,@"values", nil]; 
    NSDictionary *dict3=[[NSDictionary alloc]initWithObjectsAndKeys:@"friend",@"connect-type",nil]; 
    NSDictionary *dict4=[[NSDictionary alloc] initWithObjectsAndKeys:dict3,@"invitation-request", nil]; 
    NSDictionary *dict=[[NSDictionary alloc]initWithObjectsAndKeys:dict4,@"item-content",@"Say yes!",@"body",@"Invitation to connect.",@"subject",value,@"recipients", nil]; 
    NSLog(@"%@",[dict description]); 
    NSString *updateString = [dict 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:)];  
    [request release]; 
} 

我被困在这一点,请让我走出这一点。 - 预先感谢

+0

我有同样的问题可以帮助解决这个问题吗? – Nignesh

+0

(postUpdateApiCallResult:didFinish :) (postUpdateApiCallResult:didFail :)我应该怎么做这个方法..请帮我解决我的问题 – TamilKing

回答

3

手动调用OADataFetcher的方法。只需在设置请求的主体之前进行设置即可。

[request prepare]; 
[request setHTTPBodyWithString:updateString]; 

这对我有用。 当您使用邀​​请API时,还要从OADataFetcher中删除准备方法。

+0

我尝试了两种方式,即在两种建议的方式中调用prepare,并且我didn也没有得到任何错误信息,我也得到了成功的didFinish notifiation,但我试图连接donot的linkedin配置文件得到任何请求!你有什么主意吗?我试着在模拟器上使用电子邮件方法,真的卡住了,你有什么想法吗? –

0

Apple's code works!还有一件事,不要忘记任何标题。我仍然收到相同的消息,结果我忘了添加x-li-format和Content-type标头。

0

我从这个Linkedin thread

你只需要在代码中进行一些变化的解决方案:

NSURL *url = [NSURL URLWithString:@"http://api.linkedin.com/v1/people/~/mailbox"]; 

    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url 
    consumer:oAuthLoginView.consumer 
    token:oAuthLoginView.accessToken 
    callback:nil 
    signatureProvider:nil]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *messageToPerson = @"/people/[email protected]"; 
    NSDictionary *person = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:messageToPerson,@"_path",@"TargetFirstName",@"first-name",@"TargetSecondName",@"last-name",nil] autorelease], @"person",nil]; 
    NSArray *valueArray = [[NSArray alloc] initWithObjects:person,nil]; 
    NSDictionary *values = [[NSDictionary alloc] initWithObjectsAndKeys:valueArray,@"values", nil]; 
    NSDictionary *ir = [[NSDictionary alloc] initWithObjectsAndKeys:[[[NSDictionary alloc] initWithObjectsAndKeys:@"friend",@"connect-type",nil] autorelease], @"invitation-request",nil]; 
    NSDictionary *ic = [[NSDictionary alloc] initWithObjectsAndKeys:ir,@"item-content", nil]; 
    NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:values,@"recipients",@"Invitation",@"subject",@"ConnectWithMe",@"body", ir, @"item-content", nil]; 
    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    NSLog(@"%@",[update description]); 
    NSString *updateString = [update JSONString]; 

    [request prepare]; 

    [request setHTTPBodyWithString:updateString]; 

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

    [request release]; 

oAdata OADataFetcher.m

- (void)fetchDataWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector withId:(int)idtm
  { 

  [request release];
   
request = [aRequest retain]; 

  delegate = aDelegate;
   
didFinishSelector = finishSelector; 

  didFailSelector = failSelector; 

//note this change 

  if (idtm!=1) { 
    
  [request prepare];
   
     } 

connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
   
}
   

试试这个。肯定会工作。希望这将有助于未来的游客。

Regards

+0

这里是什么idtm? –