/// <summary>
    /// Completes the authorization process. Access tokens and list of administrated companies are retrived and set to control that opened the dialog.
    /// </summary>
    private void CompleteAuthorization()
    {
        LinkedInAccessToken token;

        try
        {
            token = LinkedInHelper.CompleteAuthorization(TokenManager);
        }
        catch (Exception ex)
        {
            LogAndShowError("LinkedInCompanyAccessToken", "AUTH_COMPLETE", ex);

            return;
        }
        finally
        {
            SessionHelper.Remove(TokenManagerStoreKey);
        }

        List <LinkedInCompany> companies;

        try
        {
            companies = LinkedInHelper.GetUserCompanies((string)Parameters["ApiKey"], (string)Parameters["ApiSecret"], token.AccessToken, token.AccessTokenSecret);
        }
        catch (Exception ex)
        {
            LogAndShowError("LinkedInCompanyAccessToken", "GET_COMPANIES", ex);

            return;
        }

        string formattedExpiration = token.Expiration.HasValue ? TimeZoneHelper.ConvertToUserTimeZone(token.Expiration.Value, true, MembershipContext.AuthenticatedUser, SiteContext.CurrentSite) : String.Empty;
        JavaScriptSerializer sr    = new JavaScriptSerializer();
        string json = sr.Serialize(
            new
        {
            accessToken           = token.AccessToken,
            accessTokenSecret     = token.AccessTokenSecret,
            tokenExpiration       = token.Expiration.HasValue ? token.Expiration.Value.ToString("g", CultureInfo.InvariantCulture) : String.Empty,
            tokenExpirationString = formattedExpiration,
            tokenAppId            = Parameters["AppInfoId"],
            companies
        }
            );

        // Set retrieved access token to the opener window
        string script = String.Format(@"
if(wopener.linkedInCompanyControl && wopener.linkedInCompanyControl['{0}']) {{
    wopener.linkedInCompanyControl['{0}'].setData({1});
}}
CloseDialog();", Parameters["ClientID"], json);

        ScriptHelper.RegisterStartupScript(Page, typeof(string), "TokenScript", script, true);
    }
 /// <summary>
 /// Begins authorization process and redirects client to the LinkedIn authorization page.
 /// </summary>
 private void BeginAuthorization()
 {
     try
     {
         // Store token manager in the session
         TokenManager = new TokenManager((string)Parameters["ApiKey"], (string)Parameters["ApiSecret"]);
         LinkedInHelper.BeginAuthorization(TokenManager, URLHelper.AddParameterToUrl(RequestContext.CurrentURL, "tokenManagerKey", TokenManagerStoreKey));
     }
     catch (LinkedInApiUnauthorizedException)
     {
         // The keys in LinkedIn application are not valid
         ShowError(GetString("sm.linkedin.account.msg.unauthorized"));
     }
     catch (Exception ex)
     {
         LogAndShowError("LinkedInCompanyAccessToken", "AUTH_BEGIN", ex);
     }
 }