C# Google AnalyticsReporting v4 API - 在 Google 示例代码段中找不到分析报告函数
C# Google AnalyticsReporting v4 API - analyticsreporting function not found in Google Example Snippet
使用这个 code snippet from google 我遇到了这个错误。我无法弄清楚我出了什么问题,因为我复制粘贴了代码片段并安装了 API 槽 nuget。
CS0103 - 名称 'analyticsreporting' 在第 50 行的当前上下文中不存在。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.AnalyticsReporting.v4;
using Google.Apis.AnalyticsReporting.v4.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace GoogleAnalytics
{
internal class Analytics
{
public Analytics(Dictionary<string, dynamic> analyticsConfig)
{
try
{
// Content here
Console.WriteLine("Hello from Google Analytics. Starting..");
// Create the DateRange object.
DateRange dateRange = new DateRange() { StartDate = "2016-02-02", EndDate = "2016-01-01" };
// Create the Metrics object.
Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };
//Create the Dimensions object.
Dimension browser = new Dimension { Name = "ga:browser" };
// Create the ReportRequest object.
ReportRequest reportRequest = new ReportRequest
{
ViewId = "my_view_id_here",
DateRanges = new List<DateRange>() { dateRange },
Dimensions = new List<Dimension>() { browser },
Metrics = new List<Metric>() { sessions }
};
List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);
// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };
// Call the batchGet method.
GetReportsResponse response = analyticsreporting.Reports.BatchGet(getReport).Execute();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
我找到了解决方案,但我认为 Google 团队可以比现在更好地记录它。以下代码演示了一个使用 oauth2 的 C# 示例。
private static async Task Run()
{
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = Settings.Default.ClientId,
ClientSecret = Settings.Default.ClientSecret
},
new[] {AnalyticsReportingService.Scope.AnalyticsReadonly},
"user",
CancellationToken.None);
var service = new AnalyticsReportingService(new BaseClientService.Initializer
{
ApplicationName = "GAReportDownloader",
HttpClientInitializer = credential
});
// Content here
Console.WriteLine("Hello from Google Analytics. Starting..");
// Create the DateRange object.
var dateRange = new DateRange {StartDate = "2017-07-01", EndDate = "2017-07-10"};
// Create the Metrics object.
var sessions = new Metric {Expression = "ga:sessions", Alias = "Sessions"};
//Create the Dimensions object.
var browser = new Dimension {Name = "ga:browser"};
// Create the ReportRequest object.
var reportRequest = new ReportRequest
{
ViewId = "YOURVIEWIDHERE",
DateRanges = new List<DateRange> {dateRange},
Dimensions = new List<Dimension> {browser},
Metrics = new List<Metric> {sessions}
};
var requests = new List<ReportRequest> {reportRequest};
// Create the GetReportsRequest object.
var getReport = new GetReportsRequest {ReportRequests = requests};
// Call the batchGet method.
var response = service.Reports.BatchGet(getReport).Execute();
Console.WriteLine();
}
}
使用这个 code snippet from google 我遇到了这个错误。我无法弄清楚我出了什么问题,因为我复制粘贴了代码片段并安装了 API 槽 nuget。
CS0103 - 名称 'analyticsreporting' 在第 50 行的当前上下文中不存在。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.AnalyticsReporting.v4;
using Google.Apis.AnalyticsReporting.v4.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace GoogleAnalytics
{
internal class Analytics
{
public Analytics(Dictionary<string, dynamic> analyticsConfig)
{
try
{
// Content here
Console.WriteLine("Hello from Google Analytics. Starting..");
// Create the DateRange object.
DateRange dateRange = new DateRange() { StartDate = "2016-02-02", EndDate = "2016-01-01" };
// Create the Metrics object.
Metric sessions = new Metric { Expression = "ga:sessions", Alias = "Sessions" };
//Create the Dimensions object.
Dimension browser = new Dimension { Name = "ga:browser" };
// Create the ReportRequest object.
ReportRequest reportRequest = new ReportRequest
{
ViewId = "my_view_id_here",
DateRanges = new List<DateRange>() { dateRange },
Dimensions = new List<Dimension>() { browser },
Metrics = new List<Metric>() { sessions }
};
List<ReportRequest> requests = new List<ReportRequest>();
requests.Add(reportRequest);
// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest() { ReportRequests = requests };
// Call the batchGet method.
GetReportsResponse response = analyticsreporting.Reports.BatchGet(getReport).Execute();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
我找到了解决方案,但我认为 Google 团队可以比现在更好地记录它。以下代码演示了一个使用 oauth2 的 C# 示例。
private static async Task Run()
{
var credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = Settings.Default.ClientId,
ClientSecret = Settings.Default.ClientSecret
},
new[] {AnalyticsReportingService.Scope.AnalyticsReadonly},
"user",
CancellationToken.None);
var service = new AnalyticsReportingService(new BaseClientService.Initializer
{
ApplicationName = "GAReportDownloader",
HttpClientInitializer = credential
});
// Content here
Console.WriteLine("Hello from Google Analytics. Starting..");
// Create the DateRange object.
var dateRange = new DateRange {StartDate = "2017-07-01", EndDate = "2017-07-10"};
// Create the Metrics object.
var sessions = new Metric {Expression = "ga:sessions", Alias = "Sessions"};
//Create the Dimensions object.
var browser = new Dimension {Name = "ga:browser"};
// Create the ReportRequest object.
var reportRequest = new ReportRequest
{
ViewId = "YOURVIEWIDHERE",
DateRanges = new List<DateRange> {dateRange},
Dimensions = new List<Dimension> {browser},
Metrics = new List<Metric> {sessions}
};
var requests = new List<ReportRequest> {reportRequest};
// Create the GetReportsRequest object.
var getReport = new GetReportsRequest {ReportRequests = requests};
// Call the batchGet method.
var response = service.Reports.BatchGet(getReport).Execute();
Console.WriteLine();
}
}