Google 分析报告 API v4 缺少必需的身份验证凭据
Google Analytics Reporting API v4 missing required authentication credential
我是这个 API 的新手,我正在努力使用 Google Analytics Reporting API v4. I am only attempting to retrieve the analytics for a website so I can build a dashboard. I am following the example here: Hello Analytics Reporting API v4; JavaScript quick start for web applications
检索我的数据
我可以提出请求,但是,我不断收到 400 error
可能是这样的:400: Invalid Credentials
`Invalid JSON payload received. Unknown name "express" at 'report_requests[0].metrics[0]': Cannot find field.
handleError @ Test.js:64
h.o0 @ cb=gapi.loaded_0:53
xs @ cb=gapi.loaded_0:56
Wq @ cb=gapi.loaded_0:56
_.C.uea @ cb=gapi.loaded_0:55
Ap @ cb=gapi.loaded_0:49`
我不知道我做错了什么,我希望有人能指出正确的方向。
这是一个反应应用程序。我在组件安装后发出请求。
import React, { Component } from 'react';
import GoogleLogin from 'react-google-login';
import $ from 'jquery';
const VIEW_ID = '17414592';
const CLIENT_ID = "936270024581-stgn130l17v21s6vjch9p751hiqbovac.apps.googleusercontent.com";
const DISC_DOCS = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ express: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}
我做错了什么?如何使用 Google Analytics Reporting APIv4 正确执行 batchRequest 以检索 7 天内的所有数据?我觉得好像我正在使用所有必填字段,但是,我不知道我缺少什么。你能给我指出正确的方向吗?
401: Invalid Credentials Invalid authorization header. The access
token you're using is either expired or invalid.
这基本上意味着您需要重新登录。
Click the Sign in button, and authorize access to Google Analytics.
请记住,访问令牌仅在一个小时内有效,过期后您将不得不重新登录。
我正在回答我自己的问题,以防其他人需要帮助使用 google 分析报告 api v4 和 React。这是我需要做的,以便构建一个 gapi 按钮,然后发出基本请求。我创建的错误是拼写错误。我使用的是 metrics:[{express: 'ga:sessions'}]
而不是 metrics:[{expression: 'ga:sessions'}]
。
这是一个将创建简单请求的组件。请注意,您必须将 VIEW_ID
更改为您的值。
import React, { Component } from 'react';
import $ from 'jquery';
const VIEW_ID = '17414592';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ expression: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}
我是这个 API 的新手,我正在努力使用 Google Analytics Reporting API v4. I am only attempting to retrieve the analytics for a website so I can build a dashboard. I am following the example here: Hello Analytics Reporting API v4; JavaScript quick start for web applications
检索我的数据我可以提出请求,但是,我不断收到 400 error
可能是这样的:400: Invalid Credentials
`Invalid JSON payload received. Unknown name "express" at 'report_requests[0].metrics[0]': Cannot find field.
handleError @ Test.js:64
h.o0 @ cb=gapi.loaded_0:53
xs @ cb=gapi.loaded_0:56
Wq @ cb=gapi.loaded_0:56
_.C.uea @ cb=gapi.loaded_0:55
Ap @ cb=gapi.loaded_0:49`
我不知道我做错了什么,我希望有人能指出正确的方向。
这是一个反应应用程序。我在组件安装后发出请求。
import React, { Component } from 'react';
import GoogleLogin from 'react-google-login';
import $ from 'jquery';
const VIEW_ID = '17414592';
const CLIENT_ID = "936270024581-stgn130l17v21s6vjch9p751hiqbovac.apps.googleusercontent.com";
const DISC_DOCS = 'https://analyticsreporting.googleapis.com/$discovery/rest?version=v4';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ express: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}
我做错了什么?如何使用 Google Analytics Reporting APIv4 正确执行 batchRequest 以检索 7 天内的所有数据?我觉得好像我正在使用所有必填字段,但是,我不知道我缺少什么。你能给我指出正确的方向吗?
401: Invalid Credentials Invalid authorization header. The access token you're using is either expired or invalid.
这基本上意味着您需要重新登录。
Click the Sign in button, and authorize access to Google Analytics.
请记住,访问令牌仅在一个小时内有效,过期后您将不得不重新登录。
我正在回答我自己的问题,以防其他人需要帮助使用 google 分析报告 api v4 和 React。这是我需要做的,以便构建一个 gapi 按钮,然后发出基本请求。我创建的错误是拼写错误。我使用的是 metrics:[{express: 'ga:sessions'}]
而不是 metrics:[{expression: 'ga:sessions'}]
。
这是一个将创建简单请求的组件。请注意,您必须将 VIEW_ID
更改为您的值。
import React, { Component } from 'react';
import $ from 'jquery';
const VIEW_ID = '17414592';
export default class Test extends Component {
constructor(props) {
super(props)
this.printResults = this.printResults.bind(this);
this.handleRequest = this.handleRequest.bind(this);
this.handleError = this.handleError.bind(this);
}
//when component mounts, create a google sign in button.
componentDidMount() {
//load gapi
$.getScript("https://apis.google.com/js/client:platform.js")
.done(() => {
window.gapi.signin2.render('my-signin2', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': this.handleRequest,
'onfailure': this.handleError
});
})
}
//on success, make a request to get google analytics data
handleRequest() {
window.gapi.client.request({
path: '/v4/reports:batchGet',
root: 'https://analyticsreporting.googleapis.com',
method: 'POST',
body: {
reportRequests: [
{
viewId: VIEW_ID,
dateRanges: [{
startDate: '7daysAgo',
endDate: 'today'
}],
metrics: [{ expression: 'ga:sessions' }]
}
]
}
}).then(this.printResults, this.handleError)
}
//log the data
printResults(response) {
console.log(response)
}
//or the error if there is one
handleError(reason) {
console.error(reason)
console.error(reason.result.error.message);
}
//render it all
render() {
return (
<div>
<div id="my-signin2"></div>
</div>
)
}
}