【问题标题】:How to get google analytics accounts list and view list api?如何获取谷歌分析帐户列表和查看列表 api?
【发布时间】:2020-04-27 09:41:39
【问题描述】:

我正在尝试编写用于从谷歌分析获取帐户列表的 API。

那么是否可以仅使用 API 密钥调用 API?

如何调用分析 api?

以下是网站中展示的示例。

const {google} = require('googleapis');

const blogger = google.blogger({
  version: 'v3',
  auth: 'YOUR API KEY'
});

const params = {
  blogId: '3213900'
};

blogger.blogs.get(params, (err, res) => {
  if (err) {
    console.error(err);
    throw err;
  }
  console.log(`The blog url is ${res.data.url}`);
});

如何调用分析来提取帐户列表、每个帐户的视图、获取指标等。

【问题讨论】:

  • 欢迎入栈请阅读stackoverflow.com/help/how-to-ask
  • 对于开始我不会使用博客 api 我会使用分析 api。
  • 我可以使用 API 密钥进行 API 调用吗?是否需要获取访问令牌?
  • 有什么方法可以获取我们账号下列出的所有浏览量和账号?

标签: node.js api google-analytics google-api google-analytics-api


【解决方案1】:

您的代码当前使用您需要使用分析 api 的博客 api。

API 密钥仅用于公共数据,您需要获得授权才能查看用户数据。 account summery list 方法将允许您查看用户有权访问的所有帐户。

var request = gapi.client.analytics.management.accountSummaries.list();
request.execute(handleResponse);

文档中有 javascript 示例,您应该不难将其和文档本身转换为 node.js 的工作代码

【讨论】:

【解决方案2】:

使用服务帐号

const {google} = require('googleapis');
const analyticsreportingv3 = google.analytics('v3');

// Inside function
const auth = new google.auth.GoogleAuth({
keyFile: '<KEYLOCATION>',
scopes: [
    'https://www.googleapis.com/auth/analytics',
    'https://www.googleapis.com/auth/analytics.readonly',
    ],
});

// Acquire an auth client, and bind it to all future calls
const authClient = await auth.getClient();
google.options({auth: authClient});

// Do the magic
const accountlist = await analyticsreportingv3.management.accountSummaries.list({

});

console.log(accountlist.data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 2015-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-15
    • 2015-08-16
    • 1970-01-01
    相关资源
    最近更新 更多