public async Task <ActionResult> Connect(Guid uniqueId, int externalApiId) { var externalApi = ExternalApi.ChartMogul.GetById(externalApiId); var externalApiAuth = new ExternalApiAuth { ExternalApiId = externalApiId }; // Replace with requested object if (uniqueId != Guid.Empty) { externalApiAuth = _externalApiAuthManager.GetExternalApiAuth(CompanyId, uniqueId); } switch (externalApi.ApiAuthorizationType) { case ExternalApiAuthorizationType.OAuth20: // Hard coded to Google for now var company = _companyManager.Get(CompanyId); Session["CompanyGuid"] = company.UniqueId.ToString(); var authResult = await _googleAuthorizer.Authorize(company.UniqueId, externalApiAuth, Url.Action("SetGoogleAnalyticsSiteId"), CancellationToken.None); if (authResult.Credential == null) { return(View("_startOAuth", new OAuthStartAuthorisation { ExternalApi = externalApi, StartUrl = authResult.RedirectUri })); } try { var accountSummaries = await _googleAuthorizer.GetAccountSummaries(authResult); if (string.IsNullOrWhiteSpace(externalApiAuth.ConfigData)) { // Default to first profile externalApiAuth.ConfigData = accountSummaries.items[0].webProperties[0].profiles[0].id; _externalApiAuthManager.Update(externalApiAuth); } return(View("_manageOAuth", new GoogleOAuthConfigView { ExternalApi = externalApi, ExternalApiAuth = externalApiAuth, Accounts = accountSummaries })); } catch { _externalApiAuthManager.Delete(externalApiAuth.Id); return(Content("We couldn't retrieve your Google Analytics site list.\nTry refreshing the page and re-connecting to Google Analytics")); } default: return(View("_connect", externalApiAuth)); } }
public async Task <MetricDataImport> GetValuesFromDataSource(int companyId, int year, int?month, int[] myMetricIds) { var metricDataImport = new MetricDataImport { success = true }; // First we get existing MetricDataPoints to cover the period specified var metricDataPoints = GetDataPointsForPeriod(companyId, year, month, myMetricIds).ToList(); // Remove any MetricDataPoints that are in the future metricDataPoints.RemoveAll(m => m.Date > DateTime.Today); var googleExternalApiAuth = _externalApiAuthManager.GetExternalApiAuth(companyId, ExternalApi.GoogleAnalytics.Id); if (googleExternalApiAuth != null) { // Get details needed for Google authorization var company = _companyManager.Get(companyId); var auth = _googleAuthorizer.Authorize(company.UniqueId, googleExternalApiAuth, "", CancellationToken.None); // Get Google data var googleAnalyticsImport = await _googleAnalyticsApiManager.GetData(companyId, _externalApiAuthManager.GetExternalApiAuth(companyId, 2), auth.Result, metricDataPoints); // Add results to local objecy metricDataImport.results.AddRange(googleAnalyticsImport.results); metricDataImport.errors.AddRange(googleAnalyticsImport.errors); } // Get ChartMogul data var chartMogulImport = await _chartMogulApiManager.GetData(companyId, _externalApiAuthManager.GetExternalApiAuth(companyId, ExternalApi.ChartMogul.Id), metricDataPoints); // Add results to the local object metricDataImport.results.AddRange(chartMogulImport.results); metricDataImport.errors.AddRange(chartMogulImport.errors); return(metricDataImport); }