LinkedIn 邀请 API 错误 401 [未经授权]-iPhone sdk

Posted

技术标签:

【中文标题】LinkedIn 邀请 API 错误 401 [未经授权]-iPhone sdk【英文标题】:LinkedIn Invitation API error 401 [unauthorized]-iPhone sdk 【发布时间】:2012-10-25 10:10:26 【问题描述】:

我正在使用 OAuth-Sample-Client 代码通过链接邀请 API 从电子邮件地址发送邀请。 我创建了与开发人员站点中描述的相同的主体,但现在我收到 401 错误,我已经搜索了很多,但没有得到任何解决方案,可以让我从任何站点或论坛中摆脱它。 我的回复如下。

<?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 字符串是这样的:

"body":"说是!","subject":"邀请 连接。","re​​cipients":"values":["person":"first-name":"test","last-name":"test","_path":"/people/email= test@test.com"],"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=test@test.com",@"_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];

我被困在这一点上,请让我摆脱困境。 -提前致谢

【问题讨论】:

我有同样的问题,谁能帮忙解决这个问题? (postUpdateApiCallResult:didFinish:) (postUpdateApiCallResult:didFail:) 我应该用这种方法做什么.. 请帮我解决我的问题 【参考方案1】:

手动调用 OADataFetcher 的 prepare 方法。只需在设置请求正文之前放置即可。

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

这对我有用。 当您使用 Invitation API 时,还要从 OADataFetcher 中删除 prepare 方法。

【讨论】:

我尝试了两种方式,即以两种建议的方式调用prepare,我也没有收到任何错误消息,我也收到了成功的didFinish通知,但是我的linkedin配置文件我试图连接没有收到任何请求!你有什么主意吗?我正在模拟器上尝试这个并使用电子邮件方法,真的卡住了,你有什么想法吗?【参考方案2】:

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

【讨论】:

【参考方案3】:

我从这个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=target@gmail.com";
    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];
        

        

试试这个。肯定会工作。希望这对未来的访客有所帮助。

问候

【讨论】:

以上是关于LinkedIn 邀请 API 错误 401 [未经授权]-iPhone sdk的主要内容,如果未能解决你的问题,请参考以下文章

使用linkedin API,这是了解我何时接受邀请的更好方法

linkedin rest api:我如何知道邀请是不是已发送?

我在 c# wpf 中使用 spotify api 收到错误 401 未授权

针对 REST API 的授权引发错误:401(未找到请求的凭据。)

调用.Net API时如何解决邮递员中的401未授权错误

尝试访问我的 api 平台时未找到 JWT 令牌 401 错误?