为了账号安全,请及时绑定邮箱和手机立即绑定

Google Analytics(分析)报告授权服务帐户

Google Analytics(分析)报告授权服务帐户

C#
ABOUTYOU 2021-03-29 21:18:15
我正在尝试使用Google Analytics(分析)API使用服务帐户在ac#webapp中报告用户信息。我已经下载了JWT文件并将其链接到项目中。我在尝试验证服务帐户时遇到问题:当我调用RequestAccessTokenAsync(cancellationToken)anc引发异常时,说作用域一定不能为空值,因为我是serviceAccountCredential根据json流创建的,所以我无法设置作用域。一种替代方法是使用来请求访问令牌,serviceAccountCredential.GetAccessTokenForRequestAsync(serviceAccountCredential.TokenServerUrl)但是在那种情况下,我不了解如何向报告请求提供该令牌,如果没有该令牌,API调用将不会被授权,并且将失败。这是源代码:string viewId = "VIEW_ID";      FileStream jwtSecretStream = new FileStream(@"path\to\servicesecret.json", FileMode.Open, FileAccess.Read);var metrics = new List<Metric> { new Metric { Expression = "ga:users" } };var dimensions = new List<Dimension> { new Dimension { Name = "ga:userType" } };DateRange dateRange = new DateRange{     StartDate = DateTime.UtcNow.AddDays(-7).ToString("yyyy-MM-dd"),     EndDate = DateTime.UtcNow.ToString("yyyy-MM-dd")};try{     serviceAccountCredential = ServiceAccountCredential.FromServiceAccountData(jwtSecretStream);     // this call will throw the exception     var x = await serviceAccountCredential.RequestAccessTokenAsync(CancellationToken.None);     //this call will return a valid token -> DON'T KNOW HOW TO PASS To THE REQUEST    //var y = await serviceAccountCredential.GetAccessTokenForRequestAsync(serviceAccountCredential.TokenServerUrl); }catch (Exception e){    throw;}_analyticsReportingService = new AnalyticsReportingService(new AnalyticsReportingService.Initializer(){    HttpClientInitializer = serviceAccountCredential,    //HttpClientInitializer = credential,    ApplicationName = "TestAnalytics",});//If I execute the request I got UNAUTHORIZED because there's not a valid access token  var response = _analyticsReportingService.Reports.BatchGet(getReportRequest).Execute();如何为服务帐户提供有效范围,或者如何请求访问令牌并将其放入批处理需求中?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信