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

调用Amazon Ads API获取配置列表时遇401未授权错误求助

调用Amazon Ads API获取配置列表时出现401 Unauthorized错误

我正在使用Google Apps Script从Amazon Ads API导入数据,已按照官方文档步骤操作,但调用获取配置列表接口时出现401 - Unauthorized错误。

获取Access Token的代码(可正常运行)

function myFunction1() {
  var url = "https://api.amazon.com/auth/o2/token";
  var client_ID = 'clientid';
  var client_secret = 'clientsecret';
  var grant_type = 'authorization_code';
  var code       ='RHwdoHCayhZMjqgSFQPI';
  var scope = 'advertising::campaign_management';
  var redirect_uri = 'https://script.google.com/a/macros/s/AKfycbwWITn81HPdSzw1RwlSz1Gy_Yo56dJdfIxom6C0eij2cSTJwjaZpJrDqCBvMxjp7dPE/exec'; 

 var payload = {
    "grant_type": grant_type,
    "client_id": client_ID,
    "client_secret": client_secret,
    "code": code, 
    "scope": scope,
    "redirect_uri": redirect_uri
  };

// Log the values before making the request
  Logger.log("code: " + code);
  Logger.log("client_id: " + client_ID);
  Logger.log("redirect_uri: " + redirect_uri);

  var options = {
    "method": "post",
    "contentType": "application/x-www-form-urlencoded",
    "payload": payload
  };

  try {
    var response = UrlFetchApp.fetch(url, options);
    if (response.getResponseCode() === 200) {
      var content = response.getContentText();
      var jsonData = JSON.parse(content);
      Logger.log("Access token obtained successfully: " + jsonData.access_token);
    } else {
      Logger.log("API request failed with response code: " + response.getResponseCode());
    }
  } catch (error) {
    Logger.log("An error occurred: " + error);
  }
}

获取配置列表的代码(出现401错误)

function myFunction2() {
var baseUrl = "https://advertising-api-eu.amazon.com/v2/profiles";
var accessToken = "Atza|IwEBIJ4XQ_3m4ftxI6cXvBnr6E8svNJHlvMnhbbW1yCI0fJfOx861qpb-usAx1p-Aa5QK9r8ZXxCa9K26R5fsE6dQMYt6n7l1aPd3RZFK1nKsRurY2eLhAf5WusGF";

var url = "https://advertising-api-eu.amazon.com/v2/profiles";

var headers = {
"Authorization": "Bearer " + accessToken
};

var options = {
"method": "get",
"headers": headers
}
var amazonResponse = UrlFetchApp.fetch(url, options);
Logger.log(amazonResponse)
}

排查建议

  • 确保Access Token未过期且正确:LWA令牌默认有效期1小时,不要手动硬编码token,建议从myFunction1的返回值动态传递到myFunction2,避免使用过期令牌。
  • 检查API区域匹配:当前调用的是欧盟区API,需确认授权账号对应的亚马逊广告账户属于欧盟区域,区域不匹配会导致权限验证失败。
  • 补充必要请求头:Amazon Ads API部分接口要求携带Amazon-Advertising-API-ClientId(即你的LWA应用client ID),尝试在请求头中添加:
    "Amazon-Advertising-API-ClientId": "你的client ID"
    
  • 验证权限范围:确认获取token时的scope为advertising::campaign_management,该权限是否覆盖/v2/profiles接口的访问权限。
  • 检查令牌格式:确认Authorization头的格式为Bearer <token>,无多余空格或字符,确保token复制完整。

内容的提问来源于stack exchange,提问作者dilip kumar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 04:04:56