C# ASP.NET GA4升级问题错误:invalid_grant for Google Analytics 4 using Google.Apis.AnalyticsReporting.v4 Nuget Package

yk9xbfzb  于 2023-10-21  发布在  .NET
关注(0)|答案(1)|浏览(105)

目前正在从Google Anaytics V3切换到V4。我收到了一个新用户的电子邮件,正在运行以下代码。

string[] scopes = { AnalyticsReportingService.Scope.AnalyticsReadonly };

//loading the Key file
var certificate = new X509Certificate2(keyFilePath, "XXXXXXXXXXXXXXXXXX", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
    Scopes = scopes
}.FromCertificate(certificate));

var data = GetGAData(credential, "52424234234", DateTime.Now.AddDays(-90), DateTime.Now);

public static GetReportsResponse GetGAData(ServiceAccountCredential credential, string viewId, DateTime startDate, DateTime endDate, string nextPageToken = null)
{
    using (var svc = new AnalyticsReportingService(
    new BaseClientService.Initializer
    {
        HttpClientInitializer = credential
    }))
{
    var mets = new List<Metric>
        {
            new Metric
            {
                Alias = "Page Views",
                Expression = "ga:pageViews"
            }
        };

    var dims = new List<Dimension>
        {
            new Dimension { Name = "ga:pagePath" }
        };

    var dateRange = new DateRange
    {
        StartDate = startDate.ToString("yyyy-MM-dd"),
        EndDate = endDate.ToString("yyyy-MM-dd")
    };

    var reportRequest = new ReportRequest
    {
        DateRanges = new List<DateRange> { dateRange },
        Dimensions = dims,
        Metrics = mets,
        ViewId = viewId,
        PageToken = nextPageToken
    };
    var getReportsRequest = new GetReportsRequest
    {
        ReportRequests = new List<ReportRequest> { reportRequest },
    };
    var batchRequest = svc.Reports.BatchGet(getReportsRequest);
    var response = batchRequest.Execute();

    return response;
}

}
我得到这个错误消息从谷歌上提供的新用户名:
消息=“错误:“invalid_grant”,说明:“无效的JWT签名。",URI:“
如果我使用V3实现中使用的旧用户名,它可以正常工作。但是,该用户绑定到一个不再使用的旧配置文件ID,因此我无法再使用该用户。

lawou6xi

lawou6xi1#

我用错了图书馆。我被这样一个事实所迷惑,即文档和nuget显示该库是一个测试版,但显然这就是你应该使用的。见下文:
https://developers.google.com/analytics/devguides/reporting/data/v1/quickstart-client-libraries

相关问题