ios - Linkedin 分享在我的应用程序中不起作用

标签 ios linkedin-api sharing

您好,我想通过我的应用程序在 LinkedIn 中共享文本。我的代码在下面...

当我单击 btn linkedIn 共享时使用此方法..

 - (void)linkedBtnEvent
    {
    if(oAuthLoginView != nil) {

        oAuthLoginView.delegate = nil;
        oAuthLoginView = nil;
    }

    oAuthLoginView = [[OAuthLoginView alloc] initWithNibName:nil bundle:nil];
    oAuthLoginView.delegate=self;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(loginViewDidFinish:)
                                                 name:@"loginViewDidFinish"
                                               object:self.oAuthLoginView];

    [self presentViewController:self.oAuthLoginView animated:YES completion:nil];
}

这是登录后处理分享的方法。

-(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];

    [self profileApiCall];
}

- (void)profileApiCall
{
    NSURL *url = [NSURL URLWithString:@"https://api.linkedin.com/v1/people/~"];
    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url
                                    consumer:oAuthLoginView.consumer
                                       token:oAuthLoginView.accessToken
                                    callback:nil
                           signatureProvider:nil];

    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];

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

}

- (void)profileApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    NSString *responseBody = [[NSString alloc] initWithData:data
                                                   encoding:NSUTF8StringEncoding];

    NSDictionary *profile = [responseBody objectFromJSONString];

    if ( profile )
    {
        NSLog(@"%@", [[NSString alloc] initWithFormat:@"%@ %@",
                      [profile objectForKey:@"firstName"], [profile objectForKey:@"lastName"]]);
    }

    // The next thing we want to do is call the network updates
    [self networkApiCall];
}

- (void)profileApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

- (void)networkApiCall
{
    NSURL *url = [NSURL URLWithString:@"https://api.linkedin.com/v1/people/~/network/updates?scope=self&count=1&type=STAT"];
    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url
                                    consumer:oAuthLoginView.consumer
                                       token:oAuthLoginView.accessToken
                                    callback:nil
                           signatureProvider:nil];

    [request setValue:@"json" forHTTPHeaderField:@"x-li-format"];

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

}

- (void)networkApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    if (isSharedLinked)
    {
        NSLog(@"Shared Successfully");
        linkedBtn.enabled = NO;
    }
    else
    {
        isSharedLinked = YES;
        [self performSelector:@selector(postTextLinkedIn) withObject:nil afterDelay:1];
    }
}

- (void)networkApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

- (void)postTextLinkedIn
{    
    NSURL *url = [NSURL URLWithString:@"https://api.linkedin.com/v1/people/~/shares"];
    OAMutableURLRequest *request = 
    [[OAMutableURLRequest alloc] initWithURL:url
                                    consumer:oAuthLoginView.consumer
                                       token:oAuthLoginView.accessToken
                                    callback:nil
                           signatureProvider:nil];

    NSDictionary *update = [[NSDictionary alloc] initWithObjectsAndKeys:
                            [[NSDictionary alloc] 
                             initWithObjectsAndKeys:
                             @"anyone",@"code",nil], @"visibility", 
                            @"Wow its working... Share the text in Linked In", @"comment", nil];

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

- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFinish:(NSData *)data 
{
    // The next thing we want to do is call the network updates
    [self networkApiCall];
}

- (void)postUpdateApiCallResult:(OAServiceTicket *)ticket didFail:(NSData *)error 
{
    NSLog(@"%@",[error description]);
}

它没有显示任何错误,它总是在 LinkedIn 中成功共享,但没有文本共享。 请帮助我解决这个问题...我不知道我犯了什么错误..

最佳答案

您需要在“scope”中添加“rw_nus”。这只允许应用程序共享更新...这是你的错误,否则你的所有代码都是正确的人..

- (void)requestTokenFromProvider
{
    OAMutableURLRequest *request = 
            [[[OAMutableURLRequest alloc] initWithURL:requestTokenURL
                                             consumer:self.consumer
                                                token:nil   
                                             callback:linkedInCallbackURL
                                    signatureProvider:nil] autorelease];

    [request setHTTPMethod:@"POST"];   

    OARequestParameter *nameParam = [[OARequestParameter alloc] initWithName:@"scope"
                                                                       value:@"r_fullprofile+r_contactinfo+r_emailaddress+r_network+r_basicprofile+rw_nus"];
    NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
    [request setParameters:params];
    OARequestParameter * scopeParameter=[OARequestParameter requestParameter:@"scope" value:@"r_fullprofile r_contactinfo r_emailaddress r_network r_fullprofile rw_nus"];

    [request setParameters:[NSArray arrayWithObject:scopeParameter]];

    OADataFetcher *fetcher = [[[OADataFetcher alloc] init] autorelease];
    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(requestTokenResult:didFinish:)
                  didFailSelector:@selector(requestTokenResult:didFail:)];    
}

将其替换为您的oAuthloginView.m

关于ios - Linkedin 分享在我的应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23285468/

相关文章:

android - 在 Android 中注销 LinkedIn session

javascript - Linkedin 分享按钮 FORM_INVALID 响应

windows-8 - 我可以使用 URL 打开 Windows 8 应用程序吗?

ios - 如何使用 RSS 为博客应用程序实现无限滚动?

ios - 如何从两个 nsarrays 创建自定义 nsdictionary

javascript - LinkedIn 如何知道或跟踪我嵌入其小部件的位置?

php - 如何使用 PHP 与 Instagram 共享链接和照片

scala - Scala 中的结构共享列表

ios - 使用 UILabel 文本掩码显示 UIView

ios - Eventkit 并不总是删除事件