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

Adsense Management API调用403权限错误:无法获取广告单元详情

问题诊断与解决方案

一、ID使用是否正确?

你当前的ID用法有误,需按以下规则调整:

  • Account ID:取API返回account['name']中的pub-xxxxxxxxxx部分(比如accounts/pub-123456789里的pub-123456789),不要带accounts/前缀。
  • AdClient ID:正确格式是ca-pub-xxxxxxxxxx,不是你用的customer id。必须先调用accounts().adclients().list(parent="accounts/{accountId}")接口拉取有效AdClient列表,从返回的adClient['name']里提取ca-pub-xxx作为adClientId。

二、403权限错误的解决步骤

1. 确认授权Scope是否实际生效

你配置的https://www.googleapis.com/auth/adsense和https://www.googleapis.com/auth/adsense.readonlyScope是正确的,但要检查:

  • 获取refresh token时,是否真的请求了这些Scope。如果授权流程里漏了,refresh token会缺失对应权限,需要重新生成token并确保授权页面同意应用访问AdSense数据。

2. 验证账号权限

  • 用于授权的Google账号必须是该AdSense账户的管理员或拥有查看/编辑权限的成员,普通账号即使有Scope也无法访问广告单元数据。
  • 确认AdSense账户已授权你的Google Cloud应用(部分场景需手动在AdSense后台关联应用)。

3. 修正API调用代码

先获取正确的AdClient ID再调用广告单元接口,示例代码:

# 先获取目标账户下的AdClient列表
adclients_req = service.accounts().adclients().list(parent=f"accounts/{accountId}")
adclients_res = adclients_req.execute()

for adclient in adclients_res.get('adClients', []):
    # 提取正确的AdClient ID
    ad_client_id = adclient['name'].split('/')[-1]
    # 调用广告单元列表接口
    adunits_req = service.accounts().adclients().adunits().list(
        parent=f"accounts/{accountId}/adclients/{ad_client_id}", pageSize=50)
    adunits_res = adunits_req.execute()
    print(adunits_res)

4. API Explorer测试注意事项

用API Explorer测试时,确保:

  • 登录的账号是目标AdSense账户的授权用户。
  • 勾选https://www.googleapis.com/auth/adsense.readonly或https://www.googleapis.com/auth/adsenseScope,并完成授权流程。

内容的提问来源于stack exchange,提问作者Gavya Mehta

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 17:12:42