You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Ads API获取启用状态广告系列返回空数组问题

解决Google Ads API返回空数组的问题

可能的问题及修复方案

  • 客户ID格式错误:Google Ads API要求客户ID必须使用带横杠的格式(如123-456-7890),你代码中使用的无分隔符数字串1234567890会导致API无法正确识别目标账户,直接返回空结果。
  • login-customer-id配置错误:如果你的账户是经理账户下的子账户,login-customer-id需要填写经理账户的ID(带横杠);如果是独立账户,可尝试移除该请求头,或确保它与客户ID格式一致(带横杠)。
  • searchStream接口响应处理不当:searchStream是流式接口,返回的是分块JSON数据,直接用JSON.parse(body)无法正确解析完整响应。建议改用非流式的search接口,更易处理返回结果。
  • 权限范围缺失:确认你的access_token已包含https://www.googleapis.com/auth/adwords权限范围,且token未过期。

修正后的代码示例

const request = require('request');

const API_VERSION = 'v13';
// 修正为带横杠的客户ID格式
const customer_id = '123-456-7890';
const developer_token = 'my-developer-token';
const access_token = 'my-access-token';
// 子账户需填经理账户ID,独立账户可填当前ID或注释该行
const login_customer_id = '123-456-7890';

const options = {
    url: `https://googleads.googleapis.com/${API_VERSION}/customers/${customer_id.replace(/-/g, '')}/googleAds:search`,
    headers: {
        'Authorization': `Bearer ${access_token}`,
        'developer-token': developer_token,
        'login-customer-id': login_customer_id.replace(/-/g, ''),
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({
        query: `SELECT campaign.id, campaign.name 
                FROM campaign 
                WHERE campaign.status = 'ENABLED'`
    })
};

request.post(options, (error, response, body) => {
    if (error) {
        console.error(error);
        return;
    }
    // 解析search接口的标准响应格式
    const result = JSON.parse(body);
    console.log(result.results);
});

内容的提问来源于stack exchange,提问作者Suraj Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 17:57:47