仅使用 API 键访问 Google 分析数据

Accessing Google Analytics Data With API Key Only

我正在尝试在 Javascript 中使用 Google 分析 API。我想:

我能找到的每个示例都要求您先使用 OAuth 进行身份验证,然后才能查询数据。因为我只想查询(不是delete/modify),有没有办法用一个简单的API键来抓取我需要的数据?

是和否。Google Analytics Core Reporting API is an Authenticated API and requires the use of OAuth2,授权范围:

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

API key 只是标识您的 Google 项目,它不一定授予您读取 Google 分析帐户数据的权限,即使该帐户与同样 Google 登录。 API 密钥通常是 public 并且存在于客户端浏览器上,如果他们有权访问特定密钥,则授予对任何人的 Google 分析帐户数据的访问权限是不安全的。

您需要做的是使用服务帐户访问数据。看一下 Google Analytics Demos and Tools site. 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. Alternatively you could build something akin to the Google Analytics Super Proxy 可以代表您查询 API 并生成 public URL 然后可以单独查询。