简体   繁体   English

仅使用 API 密钥访问 Google Analytics 数据

[英]Accessing Google Analytics Data With API Key Only

I'm trying to use Google Analytics API in Javascript.我正在尝试在 Javascript 中使用 Google Analytics API。 I want to:我想要:

  • Query Data for a specific website (under my control)查询特定网站的数据(在我的控制下)
  • Use the data to build an open-to-the-public dashboard使用数据构建向公众开放的仪表板

Every example I can find requires you to use OAuth to authenticate before you can query data.我能找到的每个示例都要求您在查询数据之前使用 OAuth 进行身份验证。 Because I only want to query (not delete/modify), is there a way to use a simple API Key to grab the data I need?因为我只想查询(而不是删除/修改),有没有办法使用简单的 API Key 来获取我需要的数据?

Yes and No. The Google Analytics Core Reporting API is an Authenticated API and requires the use of OAuth2 , authorized with the appropriate scope:是和否。Google Analytics Core Reporting API是一个经过身份验证的 API ,需要使用OAuth2 ,并在适当的范围内获得授权:

https://www.googleapis.com/auth/analytics.readonly

The API key simply identifies your Google Project, it does not necessarily grant you authorization to read the data of your Google Analytics Accounts, even if that account is associated with the same Google Login. API 密钥只是识别您的 Google 项目,并不一定授予您读取 Google Analytics 帐户数据的授权,即使该帐户与同一个 Google 登录相关联。 The API key is usually public and lives on the client browsers, it would not be secure to grant access to anyone's Google Analytics Account data simply if they had access to a particular key. API 密钥通常是公开的并且存在于客户端浏览器中,如果任何人有权访问特定密钥,则授予对任何人的 Google Analytics(分析)帐户数据的访问权限是不安全的。

What you are going to need to do is use a service account to access the data.您需要做的是使用服务帐户来访问数据。 Take a look at the Google Analytics Demos and Tools site.查看Google Analytics(分析)演示和工具网站。 There they have a really good example example of using a service account server side to generate an access token and then has the JavaScript Client library make the final request.他们有一个非常好的示例示例,使用服务帐户服务器端生成访问令牌,然后让 JavaScript 客户端库发出最终请求。 Alternatively you could build something akin to the Google Analytics Super Proxy which can query the API on your behalf and generates a public URL which can then be queried separately.或者,您可以构建类似于Google Analytics Super Proxy的东西,它可以代表您查询 API 并生成一个公共 URL,然后可以单独查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM