0

到目前为止,我的 iOS 应用程序运行良好,但今天我遇到了“我们无法找到授权令牌”的问题。有时会发生这种情况(大多数情况下它工作正常)。奇怪的是,当我尝试在我的桌面上登录我的 LinkedIn 帐户时,我第一次遇到了同样的问题。我在许多应用程序中实现了相同的代码,但所有应用程序都在工作很好。但是今天我在我的新应用程序中遇到了这个问题。

代码片段:

请求令牌:

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

    [request setHTTPMethod:@"POST"];

    LOARequestParameter *nameParam = [[LOARequestParameter alloc] initWithName:@"scope" value:@"r_fullprofile+w_messages+r_network+r_emailaddress+rw_nus"];

    NSArray *params = [NSArray arrayWithObjects:nameParam, nil];
    [request setParameters:params];

    LOARequestParameter * scopeParameter=[LOARequestParameter requestParameter:@"scope" value:@"r_fullprofile w_messages r_network r_emailaddress rw_nus"];

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

    LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(requestTokenResult:didFinish:)
                  didFailSelector:@selector(requestTokenResult:didFail:)];
}
- (void)requestTokenResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
    if (ticket.didSucceed == NO)
        return;

    NSString *responseBody = [[NSString alloc] initWithData:data
                                                   encoding:NSUTF8StringEncoding];
    self.requestToken = [[LOAToken alloc] initWithHTTPResponseBody:responseBody];

    [responseBody release];
    [self allowUserToLogin];
}

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

Linkedin 登录页面和访问令牌:

- (void)allowUserToLogin
{
    NSString *userLoginURLWithToken = [NSString stringWithFormat:@"%@?oauth_token=%@",
                                       userLoginURLString, self.requestToken.key];

           userLoginURL = [NSURL URLWithString:userLoginURLWithToken];
            NSURLRequest *request = [NSMutableURLRequest requestWithURL: userLoginURL];
            [webView loadRequest:request];
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = request.URL;
    NSString *urlString = url.absoluteString;

    addressBar.text = urlString;
    [activityIndicator startAnimating];

    BOOL requestForCallbackURL = ([urlString rangeOfString:linkedInCallbackURL].location != NSNotFound);
    if ( requestForCallbackURL )
    {
        BOOL userAllowedAccess = ([urlString rangeOfString:@"user_refused"].location == NSNotFound);
        if ( userAllowedAccess )
        {
            [self.requestToken setVerifierWithUrl:url];
            [self accessTokenFromProvider];
        }
        else
        {
            // User refused to allow our app access
            // Notify parent and close this view
//            [[NSNotificationCenter defaultCenter]
//             postNotificationName:@"loginViewDidFinish"
//             object:self
//             userInfo:nil];

            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"LinkedInCancelled"];

            [[ImpActivityAgent defaultAgent] makeBusy:NO];

            [self dismissViewControllerAnimated:NO completion:nil];
        }
    }
    else
    {
        // Case (a) or (b), so ignore it
    }
    return YES;
}
- (void)accessTokenFromProvider
{
    [[NSUserDefaults standardUserDefaults] setObject:self.consumer forKey:@"LinkedinConsumer"];

    LOAMutableURLRequest *request =
    [[[LOAMutableURLRequest alloc] initWithURL:accessTokenURL
                                     consumer:self.consumer
                                        token:self.requestToken
                                     callback:nil
                            signatureProvider:nil] autorelease];

    [request setHTTPMethod:@"POST"];
    LOADataFetcher *fetcher = [[[LOADataFetcher alloc] init] autorelease];
    [fetcher fetchDataWithRequest:request
                         delegate:self
                didFinishSelector:@selector(accessTokenResult:didFinish:)
                  didFailSelector:@selector(accessTokenResult:didFail:)];
}
- (void)accessTokenResult:(LOAServiceTicket *)ticket didFinish:(NSData *)data
{
    NSString *responseBody = [[NSString alloc] initWithData:data
                                                   encoding:NSUTF8StringEncoding];

    [[NSUserDefaults standardUserDefaults] setObject:responseBody forKey:@"AccessTokenresponseBody"];

    BOOL problem = ([responseBody rangeOfString:@"oauth_problem"].location != NSNotFound);
    if ( problem )
    {
        NSLog(@"%@",responseBody);
    }
    else
    {
        self.accessToken = [[LOAToken alloc] initWithHTTPResponseBody:responseBody];

        [[NSUserDefaults standardUserDefaults] setObject:responseBody forKey:@"accessToken"];//save here
        [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"TokenRefreshDate"];//save here
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    // Notify parent and close this view
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"loginViewDidFinish"
     object:self];

    [self dismissViewControllerAnimated:NO completion:nil];
    [responseBody release];
}

请求网址:

- (void)initLinkedInApi
{
    apikey = @"vwu3pmtjaxyz";
    secretkey = @"XkPxP1DNANMg0Dzq";

    self.consumer = [[LOAConsumer alloc] initWithKey:apikey
                                             secret:secretkey
                                              realm:@"http://api.linkedin.com/"];

    requestTokenURLString = @"https://api.linkedin.com/uas/oauth/requestToken";
    accessTokenURLString = @"https://api.linkedin.com/uas/oauth/accessToken";
    userLoginURLString = @"https://www.linkedin.com/uas/oauth/authorize";
    linkedInCallbackURL = @"hdlinked://linkedin/oauth";

    requestTokenURL = [[NSURL URLWithString:requestTokenURLString] retain];
    accessTokenURL = [[NSURL URLWithString:accessTokenURLString] retain];
    userLoginURL = [[NSURL URLWithString:userLoginURLString] retain];
}

有人可以告诉我这背后的原因是什么吗?

4

1 回答 1

0

我也将这个问题发布到了 LinkedIn 论坛,在那里我得到了他们方面的一些问题的回应,因为其他开发人员也向他们报告了这个问题。

根据 Kamyar Mohager LinkedIn 员工

当您授权新用户或尝试使用现有访问令牌拨打电话时,是否会发生这种情况?基于该错误消息,我的假设是您正在引导用户通过身份验证流程并在重定向到 LinkedIn.com 时看到错误消息。请确认。正如其他开发人员报告的那样,我们正在调查这个问题。

然后只有以下评论,他保证问题已解决。

他说:

我们的团队已经解决了通过身份验证流程重定向用户时“我们无法找到授权令牌”的问题。如果你们中的任何人继续遇到此问题,请告诉我。

于 2013-10-29T05:30:49.257 回答