I am trying to use the Google Ads API in a Node.js application, authenticating via a service account. However, I keep running into the following error:
Error: No access, refresh token, API key or refresh handler callback is set.
What I’m Trying to Achieve
I want to authenticate my Node.js application with the Google Ads API using a service account. After authenticating, I aim to create a campaign, ad group, and ad programmatically.
What I’ve Done So Far
-
Set Up Google Cloud Project:
-
Created a Google Cloud project.
-
Enabled the Google Ads API.
-
Created a service account and downloaded the JSON key file (
test12-424711-483a8f5bfc3c.json
).
-
-
Installed Required Packages:
npm install google-auth-library google-ads-api
-
Configuration File: I have a configuration file (
google-ads-auth.json
) containing necessary credentials:json
Copy code
{ "client_id": "YOUR_CLIENT_ID", "client_secret": "YOUR_CLIENT_SECRET", "developer_token": "YOUR_DEVELOPER_TOKEN", "customer_id": "YOUR_CUSTOMER_ID", "login_customer_id": "YOUR_LOGIN_CUSTOMER_ID" }
-
Code Attempt: Here’s the code I have written to authenticate and make API requests:
const { GoogleAuth } = require("google-auth-library"); const { GoogleAdsApi, enums } = require("google-ads-api"); const credentials = require("../config/google-ads-auth.json"); async function getAccessToken() { const auth = new GoogleAuth({ keyFile: "../config/test12-424711-483a8f5bfc3c.json", scopes: "https://www.googleapis.com/auth/adwords", }); const client = await auth.getClient(); const tokenResponse = await client.getAccessToken(); return tokenResponse.token; } async function createGoogleAdsApi(accessToken) { const api = new GoogleAdsApi({ client_id: credentials.client_id, client_secret: credentials.client_secret, developer_token: credentials.developer_token, access_token: accessToken, }); return api; } async function createCampaign(customer) { const campaign = { name: "My Campaign", status: enums.CampaignStatus.PAUSED, advertising_channel_type: enums.AdvertisingChannelType.SEARCH, manual_cpc: {}, }; const response = await customer.campaigns.create([campaign]); console.log("Created campaign:", response.results[0].resource_name); return response.results[0].resource_name; } async function createAdGroup(customer, campaignResourceName) { const adGroup = { name: "My Ad Group", status: enums.AdGroupStatus.PAUSED, campaign: campaignResourceName, }; const response = await customer.adGroups.create([adGroup]); console.log("Created ad group:", response.results[0].resource_name); return response.results[0].resource_name; } async function createAd(customer, adGroupResourceName) { const ad = { ad_group: adGroupResourceName, status: enums.AdStatus.PAUSED, ad: { final_urls: ["https://www.example.com"], expanded_text_ad: { headline_part1: "Example Ad", headline_part2: "Headline 2", description: "Description line", }, }, }; const response = await customer.ads.create([ad]); console.log("Created ad:", response.results[0].resource_name); } (async () => { try { const accessToken = await getAccessToken(); const api = await createGoogleAdsApi(accessToken); const customerId = credentials.customer_id; const customer = api.Customer({ customer_id: customerId, access_token: accessToken, }); const campaignResourceName = await createCampaign(customer); const adGroupResourceName = await createAdGroup(customer, campaignResourceName); await createAd(customer, adGroupResourceName); } catch (error) { console.error("Error:", error); if (error.errors) { error.errors.forEach((e) => { console.error(`${e.error_code}: ${e.message}`); }); } } })();
The Problem
Despite following the steps above, I keep encountering the error: No access, refresh token, API key or refresh handler callback is set.
What I Need Help With
I’m not sure why the authentication isn’t working. I suspect there might be an issue with how I’m handling the access token or how I’m initializing the Google Ads API client.
Any insights or guidance on how to correctly authenticate and make API requests using a service account would be greatly appreciated.