我正在做一个像flippa这样的网站来销售现有的网站,我想验证像flippa这样的网站流量,我搜索了如何获得过去12个月的访问报告,但我不能,我做什么让客户点击链接
https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=814867900881-28dlacj0v68nh9suspfjlnvjgscalaql.apps.googleusercontent.com&prompt=consent&redirect_uri=http://localhost:3000/redirect&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&state=9612
获取代码,然后使用该代码获取access_token
POST https://accounts.google.com/o/oauth2/token
code=4/gXCN77EWLDCO_fake_p2tvfakezOg6Mn0fakej2vA.giyP3fakejxeAeYFZr95uygvU3j0dumQI&
client_id=104608secret-secret-secret-secret.apps.googleusercontent.com&
client_secret=90V0FAKE_WkFAKExrHCZti&
redirect_uri=http://www.mywebapp.com/oauth2callback&
grant_type=authorization_code
然后我会得到访问令牌
{
"access_token": "ya29.Il-yBwla5jnTECDlX5rbVk_Oq4hireOUmSzeaTWW2BgYYsBCoPn6pKnQVxIUQtGc0BXblEq_2ZQ-zhrrGeL9xYYuSyd8lvRAoPBUVKUN8liBwx-w8ok2oGh2Cknql2ucew",
"expires_in": 3600,
"refresh_token": "1//031hXU_jNeKEECgYIARAAGAMSNwF-L9Irds9iUaPkcGDDOSHP2-8-1FKYBRW1GaBz1fLzEoDnEcNIm89k-bZzYWXAqie5Or-Fg94",
"scope": "https://www.googleapis.com/auth/analytics.readonly",
"token_type": "Bearer"
}
我不知道我下一步要做什么,也不知道我所做的是不是正确的方式,我需要正确的方式来获得用户GA报告
发布于 2019-11-23 12:06:18
到目前为止,您所做的工作涵盖了您的身份验证流。现在,您需要调用Analytics Reporting API并提供您创建的访问令牌。这里有一个设置请求的参考:https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet也有关于如何请求数据的示例。
发布于 2019-11-23 12:22:19
根据您的系统,这可能会有所帮助。https://developers.google.com/analytics/devguides/reporting/core/v4
本质上,您可以在google客户端中添加访问令牌,然后执行请求。
对于node.js,我找到了这个使用node.js的站点,尽管它使用的是JWT。https://flaviocopes.com/google-analytics-api-nodejs/
const { google } = require('googleapis')
const scopes = 'https://www.googleapis.com/auth/analytics.readonly'
const jwt = new google.auth.JWT(process.env.CLIENT_EMAIL, null, process.env.PRIVATE_KEY, scopes)
const view_id = 'XXXXX'
async function getData() {
const response = await jwt.authorize()
const result = await google.analytics('v3').data.ga.get({
'auth': jwt,
'ids': 'ga:' + view_id,
'start-date': '30daysAgo',
'end-date': 'today',
'metrics': 'ga:pageviews'
})
console.dir(result)
}
getData()
此外,还可以参考google APIs for Node.js:https://www.npmjs.com/package/googleapis
它包含如何将访问令牌设置为oauth2Client以及如何在API中使用这些访问令牌。
希望这能有所帮助:)
https://stackoverflow.com/questions/59004369
复制相似问题