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

如何在Google Apps Script中为Monday API传递认证令牌?

解决Google Apps Script调用Monday API的401认证问题

1. 修正请求头的认证字段

你的代码核心错误是请求头的认证字段名和格式不正确:

  • Monday API要求的认证头字段是Authorization,而非Authentication
  • 令牌值需要以Bearer 前缀开头,格式为Bearer {你的API令牌}

2. 安全存储API令牌(避免硬编码)

不要直接在代码中明文写令牌,用Google Apps Script的机密服务安全存储:

  • 打开Apps Script编辑器,点击左侧齿轮图标进入项目设置
  • 勾选显示"appsscript.json"清单文件,再点击左侧「扩展 > Apps Script API」启用API
  • 返回项目设置,切换到机密标签,点击「添加机密」,命名为MONDAY_API_TOKEN并粘贴你的Monday API令牌后保存

3. 修正后的完整代码

function requestMonday() {
  const base_url = "https://api.monday.com/v2";
  // 从机密服务读取令牌
  const api_key = PropertiesService.getScriptProperties().getProperty('MONDAY_API_TOKEN');
  const query = "query { boards (limit:1){id name} }";
  
  const headers = {
    "Authorization": `Bearer ${api_key}`, // 修正后的认证头
    "Content-Type": "application/json"
  };

  const options = {
    headers: headers,
    method: "POST",
    payload: JSON.stringify({ query: query }),
    muteHttpExceptions: true // 开启后可查看完整错误响应,便于调试
  };

  try {
    const response = UrlFetchApp.fetch(base_url, options);
    const result = JSON.parse(response.getContentText());
    console.log("result::", result);
  } catch (e) {
    console.error("请求错误:", e.toString());
  }
}

额外调试提示

  • 开启muteHttpExceptions: true后,即使请求失败也能获取完整响应内容,帮助排查权限或格式问题
  • 确认你的API令牌所属账户拥有目标看板的访问权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 19:27:33