Google Analytics Data API v1 Error 403 with access token (Nodejs)
Guillecasas1 opened this issue · comments
I am trying to access data from my users' Google Analytics 4 accounts using oauth authentication, with their access token. So far I have been accessing their Universal Analytics data and have had no problems.
I'm following this documentation:
But with the new API, using the url:
https://analyticsdata.googleapis.com/v1beta/properties/${propertyId}:runReport
It returns:
{
"error": {
"code": 403,
"message": "User does not have sufficient permissions for this property. To learn more about Property ID, see https://developers.google.com/analytics/devguides/reporting/data/v1/property-id.",
"status": "PERMISSION_DENIED"
}
}
I have tried it with my analytics account, with which I am the owner and administrator of all my properties and it also gives me the same error.
The code I am using within my NodeJS server is the following:
static async runReport (config: any) {
const { userId, propertyId } = config;
const token = await getUserGoogleAnalyticsAccessToken(userId);
const response = await fetch(
`https://analyticsdata.googleapis.com/v1beta/properties/${propertyId}:runReport`,
{
method: "POST",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
dimensions: [
{
name: 'country',
},
],
metrics: [
{
name: 'activeUsers',
},
{
name: 'totalEvents',
},
],
dateRanges: [
{
startDate: '2023-01-01',
endDate: '2023-01-31',
},
],
}),
});
console.log(response)
if (response.status === 403) {
console.log("Forbidden. Check your permissions and token.");
}
I have verified that the token is updated since I can access the Google Analytics Reporting v4 API without problem. Does anyone know how to solve it?
- The userId and propertyId variables are arriving correctly as well.
Thanks in advance.