I can't extract Google Ads campaigns via API

49 Views Asked by At

is everything going well?

I'm currently working on a system development project, and one of the key functionalities we need to implement is the ability for any user to authenticate using their Google account. Once authenticated, the software should retrieve the user's campaigns, with the intention of storing this data in a database for more advanced analysis, ultimately aiding the user in making informed decisions.

At this stage, my focus is primarily on the backend development. A fellow developer on the team has granted me administrator access to the Google Cloud Platform (GCP) App. From there, I've been able to obtain essential information, such as the client_id and client_secret. Additionally, I've been provided with a developer_token, although I'm uncertain whether I should use this token or generate my own.

Presently, I'm encountering an issue while configuring my client in NodeJS; I'm consistently receiving an "upstream request timeout" error from the API. I'm providing the client_id and client_secret obtained from the GCP App, along with the developer_token shared by my colleague. In the customer_id field, I'm passing the customer_id associated with my Google Ads account. For the refresh token, I'm using the one obtained during authentication on Google OAuth Playground.

Despite my attempts, even when incorporating a developer token from an Administrator account I created in Google Ads and using the corresponding refresh token from the playground, the error persists.

My immediate goal is to establish full functionality in the backend so that, in the subsequent frontend development phase, I can extract and transmit the refresh token to the backend. This is crucial because I need to collect refresh tokens from each user during authentication and consent to allow the app access to their campaign data. With these tokens, I can then retrieve campaigns on behalf of the user.

Source code (Firebase Functions):

index.ts

import * as functions from 'firebase-functions';
import express from 'express';
import { getCustomerList } from './controllers/GoogleAdsController';

const app = express();

app.use(express.json());

app.get('/', getCustomerList);

export const googleAds = functions.https.onRequest(app);

controllers/GoogleAdsController.ts

import { GoogleAdsApi } from "google-ads-api";
import { enums } from "google-ads-api";
import { Request, Response } from "express";

export async function getCustomerList(req: Request, res: Response) {
  const client = new GoogleAdsApi({
    client_id:
      "client_id_from_gcp_app.apps.googleusercontent.com",
    client_secret: "client_secret_from_gcp_app",
    developer_token: "developer_token",
  });

  const refreshToken = "refresh_token_from_playground";

  const customer = client.Customer({
    customer_id: "customer_id",
    refresh_token: refreshToken,
  });

  try {
    const campaigns = await customer.report({
        entity: "campaign",
        attributes: [
          "campaign.id",
          "campaign.name",
          "campaign.bidding_strategy_type",
          "campaign_budget.amount_micros",
        ],
        metrics: [
          "metrics.cost_micros",
          "metrics.clicks",
          "metrics.impressions",
          "metrics.all_conversions",
        ],
        constraints: {
          "campaign.status": enums.CampaignStatus.ENABLED,
        },
        limit: 20,
      });
    res.send(campaigns);
  } catch (error) {
    res.status(500).send(error);
  }
}

I'm trying to troubleshoot why the campaigns are not returning, and I'm in need of some assistance. Here's what I've tried so far:

  1. I've ensured that I'm already an administrator on the GCP App, and I have the necessary credentials, including the client_id and client_secret.

  2. Despite trying both the developer_token provided by the other developer and ones I've configured myself, the error persists.

  3. I'm obtaining the refresh token during authentication on the Google OAuth Playground using this link: OAuth Playground Link.

  4. I've attempted to use various customer_id values, including my own and the other developer's, but the error remains unchanged.

  5. I've been grappling with this issue for two weeks now, and I've conducted extensive research, including following the guidance on GitHub regarding the NodeJS client, without any success. I've been referring to the repository at GitHub Repository Link.

If anyone has encountered a similar situation or has experience with this, your insights would be greatly appreciated. This feature is crucial for the system, and I'm eager to resolve it. Thank you very much for your assistance.

0

There are 0 best solutions below