无法使用服务帐户令牌生成 Google Analytics DataChart 客户端(无效凭据错误)
Can't generate Google Analytics DataChart client side with Service Account Token (Invalid Credentials Error)
我正在尝试通过他们的 Javascript API 实施 Google 分析图 ,作为他们网站上的示例 link.
但每次我尝试执行 gapi.analytics.googleCharts.DataChart 时,我总是收到 "401 Invalid Credentials"
我正在使用以下代码获取访问令牌服务器端 (C#),其中包含为服务帐户
生成的 JSON 中的数据
var cred = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(clientId)
{
Scopes = new[] { AnalyticsReportingService.Scope.AnalyticsReadonly },
User = clientEmail,
ProjectId = "projectID"
}.FromPrivateKey(privateKey));
var token = cred.GetAccessTokenForRequestAsync(authURI);
token.Wait();
var result = token.Result;
return result;
或(使用完整的 json 字符串,也请参见注释)
GoogleCredential cred;
var gCred = GoogleCredential.FromJson(json).UnderlyingCredential as
ServiceAccountCredential;
var token = gCred.GetAccessTokenForRequestAsync("https://accounts.google.com/o/oauth2/auth");
return token.Result;
在客户端
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ ACCESS_TOKEN_FROM_SERVICE_ACCOUNT }}'
}
});
通过并使用 gapi.analytics.auth.isAuthorized() returns true 使用任何服务器端函数,但在尝试调用时失败
var dataChart1 = new gapi.analytics.googleCharts.DataChart(queryJson);
dataChart1.execute();
returns 401 "Invalid Credentials",服务器端查询 returns 值很好所以我认为用户权限不是问题
注意:使用与第二个代码相同的代码(使用 json 字符串生成凭据而不转换为 ServiceAccountCredential)我可以从 API 服务器端获取数据
cred = gCred.CreateScoped(scopes);
using (var reportingService = new AnalyticsReportingService(new BaseClientService.Initializer
{
HttpClientInitializer = cred
}))
...
var getReportsRequest = new GetReportsRequest
{
ReportRequests = new List<ReportRequest> { reportRequest }
};
var batchRequest = reportingService.Reports.BatchGet(getReportsRequest);
var response = batchRequest.Execute(); //This returns a response object with all the data I need
如果有人遇到同样的问题:
您需要使用 GoogleCredential.GetApplicationDefault() 来创建 Credential 对象,对于 Analytics,您应该得到像这样
var credential = GoogleCredential.GetApplicationDefault().CreateScoped(AnalyticsReportingService.Scope.AnalyticsReadonly);
这将从 Windows 上的 环境变量 中获取 json 文件,因此您需要设置 GOOGLE_APPLICATION_CREDENTIALS 将 json 的路径作为系统变量。
初始化服务并设置服务变量
using (var reportingService = new AnalyticsReportingService(new BaseClientService.Initializer { HttpClientInitializer = credential }))
{
var serviceCredential = cred.UnderlyingCredential as ServiceAccountCredential;
}
我正在尝试通过他们的 Javascript API 实施 Google 分析图 ,作为他们网站上的示例 link.
但每次我尝试执行 gapi.analytics.googleCharts.DataChart 时,我总是收到 "401 Invalid Credentials"
我正在使用以下代码获取访问令牌服务器端 (C#),其中包含为服务帐户
生成的 JSON 中的数据var cred = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(clientId)
{
Scopes = new[] { AnalyticsReportingService.Scope.AnalyticsReadonly },
User = clientEmail,
ProjectId = "projectID"
}.FromPrivateKey(privateKey));
var token = cred.GetAccessTokenForRequestAsync(authURI);
token.Wait();
var result = token.Result;
return result;
或(使用完整的 json 字符串,也请参见注释)
GoogleCredential cred;
var gCred = GoogleCredential.FromJson(json).UnderlyingCredential as
ServiceAccountCredential;
var token = gCred.GetAccessTokenForRequestAsync("https://accounts.google.com/o/oauth2/auth");
return token.Result;
在客户端
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ ACCESS_TOKEN_FROM_SERVICE_ACCOUNT }}'
}
});
通过并使用 gapi.analytics.auth.isAuthorized() returns true 使用任何服务器端函数,但在尝试调用时失败
var dataChart1 = new gapi.analytics.googleCharts.DataChart(queryJson);
dataChart1.execute();
returns 401 "Invalid Credentials",服务器端查询 returns 值很好所以我认为用户权限不是问题
注意:使用与第二个代码相同的代码(使用 json 字符串生成凭据而不转换为 ServiceAccountCredential)我可以从 API 服务器端获取数据
cred = gCred.CreateScoped(scopes);
using (var reportingService = new AnalyticsReportingService(new BaseClientService.Initializer
{
HttpClientInitializer = cred
}))
...
var getReportsRequest = new GetReportsRequest
{
ReportRequests = new List<ReportRequest> { reportRequest }
};
var batchRequest = reportingService.Reports.BatchGet(getReportsRequest);
var response = batchRequest.Execute(); //This returns a response object with all the data I need
如果有人遇到同样的问题:
您需要使用 GoogleCredential.GetApplicationDefault() 来创建 Credential 对象,对于 Analytics,您应该得到像这样
var credential = GoogleCredential.GetApplicationDefault().CreateScoped(AnalyticsReportingService.Scope.AnalyticsReadonly);
这将从 Windows 上的 环境变量 中获取 json 文件,因此您需要设置 GOOGLE_APPLICATION_CREDENTIALS 将 json 的路径作为系统变量。
初始化服务并设置服务变量
using (var reportingService = new AnalyticsReportingService(new BaseClientService.Initializer { HttpClientInitializer = credential }))
{
var serviceCredential = cred.UnderlyingCredential as ServiceAccountCredential;
}