gpt4 book ai didi

google-analytics - Google Analytics Reporting API v4缺少必需的身份验证凭据

转载 作者:行者123 更新时间:2023-12-03 16:05:16 24 4
gpt4 key购买 nike

我是使用此API的新手,并且正在努力使用Google Analytics Reporting API v4检索数据。我仅尝试检索网站的分析数据,因此可以构建仪表板。我在这里跟随示例: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`


我不知道我在做什么错,我希望有人能指出我正确的方向。

这是一个React应用程序。组件安装后,我正在发出请求。

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天之内的所有数据?我感觉好像我在使用所有必填字段,但是,我不知道自己缺少什么。你能为我指出正确的方向吗?

最佳答案

我正在回答我自己的问题,以防其他人需要使用带有响应的Google Analytics Reporting API v4的帮助。这是我需要做的,以便构造一个带间隙的按钮,然后发出基本请求。我创建的错误是由错字引起的。我使用的是metrics:[{expression: 'ga:sessions'}]而不是metrics:[{express: '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>
)
}
}

关于google-analytics - Google Analytics Reporting API v4缺少必需的身份验证凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43081532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com