Reputation: 62
I'm building a Web Api and I need to get data from a Google Analytics report. I need to get data from a Google Analytics view. But I think I'm facing an issue with the credentials.
Here is the code I'm using.
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));
}
I'm getting the following issue:
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]
Thanks, Andrés
Upvotes: 0
Views: 109
Reputation: 164
Make sure the credentials´ user has access to the Google Analytics account you are trying to access. You must grant it access in the analytics page, just as you would for any other user.
Upvotes: 1