gpt4 book ai didi

c# - 从 Web Api 获取 Google Analytics 报告中的数据

转载 作者:行者123 更新时间:2023-12-02 00:58:49 25 4
gpt4 key购买 nike

我正在构建一个 Web Api,我需要从 Google Analytics 报告中获取数据。我需要从 Google Analytics View 获取数据。但我认为我面临着凭据问题。

这是我正在使用的代码。

        var filepath = "XXXXXXXXXXXXXXXXx";  
var viewid = "XXXXXXXXXX";
GoogleCredential credentials;
using (var stream = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
string[] scopes = { AnalyticsReportingService.Scope.AnalyticsReadonly };
var googleCredential = GoogleCredential.FromStream(stream);
credentials = googleCredential.CreateScoped(scopes);
}

var reportingService = new AnalyticsReportingService(
new BaseClientService.Initializer
{
HttpClientInitializer = credentials
});
var dateRange = new DateRange
{
StartDate = "2018-06-01",
EndDate = "2018-06-25"
};
var sessions = new Metric
{
Expression = "ga:pageviews",
Alias = "Sessions"
};
var date = new Dimension { Name = "ga:date" };

var reportRequest = new ReportRequest
{
DateRanges = new List<DateRange> { dateRange },
Dimensions = new List<Dimension> { date },
Metrics = new List<Metric> { sessions },
ViewId = viewid
};
var getReportsRequest = new GetReportsRequest
{
ReportRequests = new List<ReportRequest> { reportRequest }
};
var batchRequest = reportingService.Reports.BatchGet(getReportsRequest);
var response = batchRequest.Execute();
foreach (var x in response.Reports.First().Data.Rows)
{
Console.WriteLine(string.Join(", ", x.Dimensions) + " " + string.Join(", ", x.Metrics.First().Values));
}

我遇到以下问题:

  Google.GoogleApiException: 'Google.Apis.Requests.RequestError
User does not have any Google Analytics account. [403]
Errors [
Message[User does not have any Google Analytics account.] Location[ - ]
Reason[forbidden] Domain[global]

谢谢,安德烈斯

最佳答案

确保凭据用户有权访问您尝试访问的 Google Analytics(分析)帐户。您必须在分析页面中授予其访问权限,就像授予任何其他用户一样。

关于c# - 从 Web Api 获取 Google Analytics 报告中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51519258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com