Search code examples
ioslinkedin-api

We were unable to find the authorization token on LinkedIn login in iOS sdk


I have my iOS app working fine till now but today I faced the problem "We were unable to find the authorization token." which is happening sometimes(most of the time it is working fine).Strangely when I tried to login my LinkedIn account on my desktop I faced the same issue for the first time.I have been implementing same code in many apps but all were working fine.But today I got this issue in my new app.

Code Snippet:

Request Token:

- (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 Login Page And Access Token:

- (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];
}

Request Urls:

- (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];
}

Can somebody please tell me what can be reason behind it?


Solution

  • I have posted this question to LinkedIn forum too where I got the response that there was some issue from their side as other other developers have also reported them the issue.

    As per Kamyar Mohager LinkedIn Employee

    Is this happening when you're authorizing a new user or are you trying to make calls with existing access tokens? Based on that error message, my assumption is that you're taking users through the auth flow and seeing the error message upon redirect to LinkedIn.com. Please confirm. We're looking into this issue as other developers have reported it.

    Then only the following comment he has assured that the problem is resolved.

    He Said:

    Our team has resolved the "We were unable to find the authorization token" issue when redirecting users through the auth flow. Please let me know if any of you continue to experience this issue.