Я пытаюсь отобразить диаграммы с использованием разрешения на стороне сервера в C#, но я не могу это сделать.Google Analytics Внедрить серверный интерфейс сервера API, не отображая диаграммы с C#
Google есть пример, но основанный на Python, и мне нужно строить на основе C# MVC: https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/
Я создал учетную запись службы и загрузить файл в формате JSON:
Controler
public class StatsController : Controller
{
// GET: Stats
public async Task<ActionResult> Index()
{
var json = "C:\\temp\\client_secrets.json";
string[] scopes = new string[] { AnalyticsReportingService.Scope.AnalyticsReadonly }; // Put your scopes here
var stream = new FileStream(json, FileMode.Open, FileAccess.Read);
var credential = GoogleCredential.FromStream(stream);
credential = credential.CreateScoped(scopes);
try
{
Task<string> task = ((ITokenAccess)credential).GetAccessTokenForRequestAsync();
task.Wait();
var bearer = task.Result;
ViewBag.Token = bearer;
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
return View();
}
}
Просмотр
<script>
(function(w,d,s,g,js,fs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>
<div id="chart-1-container"></div>
<script>
gapi.analytics.ready(function() {
/**
* Authorize the user with an access token obtained server side.
*/
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ @ViewBag.Token }}'
}
});
/**
* Creates a new DataChart instance showing sessions over the past 30 days.
* It will be rendered inside an element with the id "chart-1-container".
*/
var dataChart1 = new gapi.analytics.googleCharts.DataChart({
query: {
'ids': 'ga:XXXX', // <-- Replace with the ids value for your view.
'start-date': '30daysAgo',
'end-date': 'yesterday',
'metrics': 'ga:sessions,ga:users',
'dimensions': 'ga:date'
},
chart: {
'container': 'chart-1-container',
'type': 'LINE',
'options': {
'width': '100%'
}
}
});
dataChart1.execute();
});
</script>
Ничто не делает, и я получаю с различными маркерами каждый раз я обновить вид, и весь этот erros в консоли:
Подробно
Я бы не стал беспокоиться о том, что токен отличается. Можете ли вы опубликовать полную ошибку 401 – DaImTo
Привет, спасибо! Пожалуйста, проверьте обновленный вопрос с деталями – Patrick
@DaImTo помогло ли вам отпечаток? – Patrick