iOS中的LinkedIn集成:邀请API不起作用

D_D*_*D_D 2 iphone objective-c linkedin ios linkedin-jsapi

我正在尝试在我的iOS应用程序中集成LinkedIn,我需要进行身份验证,然后获取个人资料详细信息并从我的应用程序发布到LinkedIn.我使用这个示例项目LinkedIn OAuth示例客户端作为参考非常成功地做了所有这些事情,我做了一些修改,一切都运行得很好.我也使用LinnkedIn API中的细节 来实现这一点.接下来,我需要从同一个应用程序发送连接到LinkedIn的邀请.我尝试使用Invitataion API.

这是我的代码片段:

-(void)sendInvitationToconnectTo:(NSString *)emailAddress withMessage:(NSString *)text

{

    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=userName@gmail.com",@"_path",@"userName1",@"first-name",@"userName2",@"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];

    NSString *updateString = [dict JSONString];
    NSLog(@"updateString : %@",updateString);

    [request setHTTPBodyWithString:updateString];
    [request setHTTPMethod:@"POST"];
    OADataFetcher *fetcher = [[OADataFetcher alloc] init];
    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(sendRequestApiCallResult:didFinish:)
                  didFailSelector:@selector(sendRequestApiCallResult:didFail:)];
    [request release];
}



- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data

{
    NSLog(@"invitation sent");
    [self networkApiCall];
}

- (void)sendRequestApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error

{
    NSLog(@"%@",[error description]);
}
Run Code Online (Sandbox Code Playgroud)

我的json字符串看起来像这样:

{"body":"Say yes!","subject":"邀请连接.","收件人":{"values":[{"person":{"first-name":"userName1","last" -name ":" USERNAME2" , "_路径": "/ people/email=userName@gmail.com"}}]}, "项内容":{ "邀请请求":{ "连接型":"朋友"}}}

在发送邀请时,我没有收到任何错误,当我调试时,该sendRequestApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data方法被调用显示邀请发送文本.但问题是目标LinkedIn用户没有得到任何请求!

任何人都可以帮我这个吗?我的代码中有什么问题吗?

NB我通过以下提到的链接获得解决方案,尝试了解决方案,但徒劳无功.

1. 邀请API - 被401(iOS和OAuthConsumer)难倒 - 已解决
2. linkedin connect API给出错误401 - iphone

NSURLResponse我得到的StatusCode是401!

编辑::我自己解决了.请参阅下面的答案.

D_D*_*D_D 5

很高兴通知所有经过长达4天的连续研究,我解决了这个问题!我从这个Linkedin线程得到了解决方案

您只需要在代码中进行一些更改:

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];
Run Code Online (Sandbox Code Playgroud)

并在 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];?    
}?    
Run Code Online (Sandbox Code Playgroud)

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

问候