引用此文档 JSON Web Tokens ,我尝试访问 Analytics Reporting API,首先尝试“应用程序默认凭据”,然后尝试“JSON Web token ”身份验证。
每次我收到错误 Request had insufficient authentication scopes.
我通过 Query Explorer 访问数据没有问题,而且我真的不想使用 OAuth2,因为我一开始只会访问自己的帐户。下面列出的范围来自 Google scopes 中的“Google Analytics API, v3” .
鉴于错误消息,我尝试了其他范围的各种迭代,最值得注意的是“Analytics Reporting API,v4”。
备注 :Google Developers Console 的服务帐户 key 中提供的电子邮件已添加到 Analytics 管理控制台的所有权限(帐户、属性、 View )中。我也尝试过类似“Service <--> Service authentication”中描述的方法。
尝试“应用程序默认凭据”(在 .env 中设置服务帐户 key 的路径):
const {auth} = require('google-auth-library');
async function main() {
const client = await auth.getClient({
scopes: [
'https://www.googleapis.com/auth/analytics',
'https://www.googleapis.com/auth/analytics.edit',
'https://www.googleapis.com/auth/analytics.manage.users',
'https://www.googleapis.com/auth/analytics.manage.users.readonly',
'https://www.googleapis.com/auth/analytics.provision',
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/analytics.user.deletion'
]
});
const projectId = await auth.getProjectId();
const url = `https://www.googleapis.com/dns/v1/projects/${projectId}`;
const res = await client.request({ url });
console.log(res.data);
}
main().catch(console.error);
尝试“JSON Web token :”
const {JWT} = require('google-auth-library');
const keys = require('../service-account-credentials.json');
async function main() {
console.log('keys.client_email: ', keys.client_email)
console.log('keys.private_key: ', keys.private_key)
const client = new JWT(
keys.client_email,
null,
keys.private_key,
[
'https://www.googleapis.com/auth/analytics',
'https://www.googleapis.com/auth/analytics.edit',
'https://www.googleapis.com/auth/analytics.manage.users',
'https://www.googleapis.com/auth/analytics.manage.users.readonly',
'https://www.googleapis.com/auth/analytics.provision',
'https://www.googleapis.com/auth/analytics.readonly',
'https://www.googleapis.com/auth/analytics.user.deletion'
],
);
const url = `https://www.googleapis.com/dns/v1/projects/${keys.project_id}`;
const res = await client.request({url});
console.log(res.data);
const tokenInfo = await client.getTokenInfo(client.credentials.access_token);
console.log(tokenInfo);
}
main().catch(console.error);
请您参考如下方法:
样板示例中指定的 url 需要替换为分析查询,例如来自 "API Query URI" 的查询.
例如,如果我想要页面浏览量,我将替换问题片段中使用的 url:https://www.googleapis.com/dns/v1/projects/${projectId}
使用查询资源管理器中指定的内容:https://www.googleapis.com/analytics/v3/data/ga?ids=ga%${analyticsViewOrProfileId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews
结果(功能)代码如下:
const {auth} = require('google-auth-library');
async function main() {
const client = await auth.getClient({
scopes: [
'https://www.googleapis.com/auth/analytics',
'https://www.googleapis.com/auth/analytics.readonly'
]
});
const viewId = <VIEW ID FROM ANALYTICS CONSOLE>
const url = `https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3A${viewId}&start-date=30daysAgo&end-date=2018-08-28&metrics=ga%3Apageviews&dimensions=ga%3ApagePath&sort=-ga%3Apageviews`;
const res = await client.request({ url });
console.log(res.data);
}
main().catch(console.error);


