2012-10-25 49 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? –