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

使用Office Script(TS)调用Salesforce Marketing Cloud API遇请求失败问题

问题分析与解决方案

你在使用Office Script(TypeScript)调用Salesforce Marketing Cloud的令牌接口时触发Failed to fetch错误,但相同的请求逻辑在Google Colab的Python环境中可以正常运行。

核心问题

你的代码存在两个关键错误:

  1. 使用了GET请求方法,而Salesforce Marketing Cloud的令牌接口要求必须用POST方法提交认证参数。
  2. 未携带任何身份验证参数(client_id、client_secret等),接口无法识别你的请求合法性。

修正后的代码示例

async function main(workbook: ExcelScript.Workbook) {
    // 替换为你的Salesforce Marketing Cloud认证信息
    const clientId = "你的Client ID";
    const clientSecret = "你的Client Secret";
    const tokenUrl = "https://**********.auth.marketingcloudapis.com/v2/token";

    try {
        const fetchResult = await fetch(tokenUrl, {
            method: "POST",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify({
                grant_type: "client_credentials",
                client_id: clientId,
                client_secret: clientSecret
            })
        });

        if (!fetchResult.ok) {
            throw new Error(`请求失败,状态码: ${fetchResult.status}`);
        }

        const tokenData = await fetchResult.json();
        console.log("令牌获取成功:", tokenData);
    } catch (error) {
        console.error("请求出错:", error.message);
    }
}

额外注意事项

  • 确保替换代码中的你的Client ID和你的Client Secret为真实的Salesforce Marketing Cloud凭证。
  • 检查Office Script的网络权限设置,确认允许访问目标API域名。
  • 如果仍遇到跨域相关错误,需要在Salesforce Marketing Cloud后台配置允许Office域名(如*.officeapps.live.com)作为合法跨域来源。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 11:47:39