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

亚历克斯·法伦斯泰特

我是使用这个 API 的新手,我正在努力使用Google Analytics Reporting API v4检索我的数据我只是尝试检索网站的分析,以便我可以构建仪表板。我正在关注这里的示例:Hello Analytics Reporting API v4;Web 应用程序的 JavaScript 快速入门

我能够提出请求,但是,我不断收到400 error可能是这样的:400:无效的凭据

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

亚历克斯·法伦斯泰特

我正在回答我自己的问题,以防其他人需要帮助使用带有反应的 google 分析报告 api v4。这是我需要做的,以便构建一个 gapi 按钮,然后发出基本请求。我创建的错误来自一个错字。而不是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>
    )
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何通过Google Analytics(分析)Reporting API v4进行身份验证

来自分类Dev

Google Analytics Reporting API V3 / V4与Google Analytics 4(GA4)的兼容性

来自分类Dev

Analytics Reporting API V4 API问题

来自分类Dev

Analytics Reporting API v4(Java)中的指标类

来自分类Dev

添加 ga:pagePath 维度以使用 Google Analytics Reporting API v4 获取特定 URL 的页面浏览量

来自分类Dev

Google Analytics Reporting API v3要求用户登录

来自分类Dev

Analytics v4更新后,如何使用webDataStream从Analytics Reporting Api获取数据?

来自分类Dev

Google Analytics v4和Google API

来自分类Dev

Google Analytics API V4:includeEmptyRows:'true',不工作

来自分类Dev

.NET的Analytics Reporting API V4客户端库

来自分类Dev

错误:未在项目中使用Analytics Reporting API V4

来自分类Dev

过滤来自Google Analytics Reporting API的结果

来自分类Dev

Google Analytics Reporting API投掷401

来自分类Dev

Google Analytics Embed API身份验证

来自分类Dev

使用Core Reporting Google API v4(Java)显示超过10000行

来自分类Dev

使用Core Reporting Google API v4(Java)显示超过10000行

来自分类Dev

以前使用 Google Reporting API v4 工作的 Python 脚本现在返回 403

来自分类Dev

如何刷新Google Analytics Reporting API v3的访问令牌

来自分类Dev

如何刷新Google Analytics Reporting API v3的访问令牌

来自分类Dev

在使用Python Requests模块向YouTube API v3发出POST请求时,为什么会收到“请求缺少必需的身份验证凭据”的信息?

来自分类Dev

升级到Google Analytics(分析)API v4,而不是等待新的alpha API可用?

来自分类Dev

通过ID查询Google Analytics(分析)Reporting API

来自分类Dev

以 XML/JSON 格式返回 Google Analytics Reporting API 结果

来自分类Dev

laravel-Google Analytics(分析)API身份验证

来自分类Dev

Google Analytics(分析):通过身份验证执行API

来自分类Dev

laravel-Google Analytics(分析)API身份验证

来自分类Dev

google Analytics V4无法导入?

来自分类Dev

使用PHP的Google Analytics(分析)API v4。订购输出

来自分类Dev

适用于Android的Google Analytics API v4无法发送屏幕视图

Related 相关文章

  1. 1

    如何通过Google Analytics(分析)Reporting API v4进行身份验证

  2. 2

    Google Analytics Reporting API V3 / V4与Google Analytics 4(GA4)的兼容性

  3. 3

    Analytics Reporting API V4 API问题

  4. 4

    Analytics Reporting API v4(Java)中的指标类

  5. 5

    添加 ga:pagePath 维度以使用 Google Analytics Reporting API v4 获取特定 URL 的页面浏览量

  6. 6

    Google Analytics Reporting API v3要求用户登录

  7. 7

    Analytics v4更新后,如何使用webDataStream从Analytics Reporting Api获取数据?

  8. 8

    Google Analytics v4和Google API

  9. 9

    Google Analytics API V4:includeEmptyRows:'true',不工作

  10. 10

    .NET的Analytics Reporting API V4客户端库

  11. 11

    错误:未在项目中使用Analytics Reporting API V4

  12. 12

    过滤来自Google Analytics Reporting API的结果

  13. 13

    Google Analytics Reporting API投掷401

  14. 14

    Google Analytics Embed API身份验证

  15. 15

    使用Core Reporting Google API v4(Java)显示超过10000行

  16. 16

    使用Core Reporting Google API v4(Java)显示超过10000行

  17. 17

    以前使用 Google Reporting API v4 工作的 Python 脚本现在返回 403

  18. 18

    如何刷新Google Analytics Reporting API v3的访问令牌

  19. 19

    如何刷新Google Analytics Reporting API v3的访问令牌

  20. 20

    在使用Python Requests模块向YouTube API v3发出POST请求时,为什么会收到“请求缺少必需的身份验证凭据”的信息?

  21. 21

    升级到Google Analytics(分析)API v4,而不是等待新的alpha API可用?

  22. 22

    通过ID查询Google Analytics(分析)Reporting API

  23. 23

    以 XML/JSON 格式返回 Google Analytics Reporting API 结果

  24. 24

    laravel-Google Analytics(分析)API身份验证

  25. 25

    Google Analytics(分析):通过身份验证执行API

  26. 26

    laravel-Google Analytics(分析)API身份验证

  27. 27

    google Analytics V4无法导入?

  28. 28

    使用PHP的Google Analytics(分析)API v4。订购输出

  29. 29

    适用于Android的Google Analytics API v4无法发送屏幕视图

热门标签

归档