Trouble Authenticating Google Ads API with Service Account in Node.js

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

  1. 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).

  2. Installed Required Packages:

    npm install google-auth-library google-ads-api

  3. 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" }

  4. 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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật