I am trying to retrieve google performance using the google business performance api based on the documentation of google. I got into this error which says
[Nest] 17735 - 04/19/2023, 5:43:22 AM ERROR [ExceptionsHandler] The caller does not have permission
Error: The caller does not have permission
at Gaxios._request (/Users/nirjalpaudel/Programming/hifoods/node_modules/.pnpm/[email protected]/node_modules/gaxios/src/gaxios.ts:158:15)
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at OAuth2Client.requestAsync (/Users/nirjalpaudel/Programming/hifoods/node_modules/.pnpm/[email protected]/node_modules/google-auth-library/build/src/auth/oauth2client.js:382:18)
at RestaurantPerformanceService.getPerformance (/Users/nirjalpaudel/Programming/hifoods/src/modules/restaurant-performance/restaurant-performance.service.ts:35:18)
My code base for the api looks something like this
import { BadRequestException, Injectable } from "@nestjs/common";
import { Auth, google } from "googleapis";
import { ConfigService } from "@nestjs/config";
@Injectable()
export class RestaurantPerformanceService {
OAUTH2_CLIENT_ID = this.configService.get("OAUTH2_CLIENT_ID");
OAUTH2_CLIENT_SECRET = this.configService.get("OAUTH2_CLIENT_SECRET");
OAUTH2_REDIRECT_URL = this.configService.get("OAUTH2_REDIRECT_URL");
oauth2Client: Auth.OAuth2Client = new google.auth.OAuth2({
clientId: this.OAUTH2_CLIENT_ID,
clientSecret: this.OAUTH2_CLIENT_SECRET,
redirectUri: this.OAUTH2_REDIRECT_URL,
});
SCOPES = ["https://www.googleapis.com/auth/business.manage"];
constructor(private readonly configService: ConfigService) {}
async callBack(code: string) {
try {
const { tokens } = await this.oauth2Client.getToken(code);
this.oauth2Client.setCredentials(tokens);
return { tokens };
} catch (e) {
throw new BadRequestException(e);
}
}
async getPerformance(token: Auth.Credentials, placeId: string) {
// Acquire an auth client, and bind it to all future calls
this.oauth2Client.setCredentials(token);
const resp = await google
.businessprofileperformance({
version: "v1",
auth: this.oauth2Client,
})
.locations.fetchMultiDailyMetricsTimeSeries({
dailyMetrics: ["WEBSITE_CLICKS"],
"dailyRange.endDate.day": 1,
"dailyRange.endDate.month": 1,
"dailyRange.endDate.year": 2023,
"dailyRange.startDate.day": 1,
"dailyRange.startDate.month": 1,
"dailyRange.startDate.year": 2023,
location: `locations/${placeId}`,
});
return resp.data;
}
async createOauthLogin() {
try {
const authUrl = this.oauth2Client.generateAuthUrl({
access_type: "offline",
prompt: "consent",
scope: this.SCOPES,
client_id: this.OAUTH2_CLIENT_ID,
});
return { authUrl };
} catch (e) {
throw new BadRequestException(e);
}
}
}
I have tried to fix this issue via various other stack overflow questions, but could not find the one which fixes my problem. Can you help me.
The flow look something like this
- createOauthLogin()
- callback()
- getPerformance()