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

使用Facebook NodeJS Business SDK上传AdImage遇文件无效错误

Facebook NodeJS Business SDK调用createAdImage报错"The provided image file is invalid"的解决方法

问题背景

使用广告沙箱账户的API密钥调用Facebook NodeJS Business SDK的createAdImage方法时,收到错误提示"The provided image file is invalid",但使用相同参数的curl请求能正常成功。

错误详情

{
  "message": "The provided image file is invalid: The provided image file is invalid. Please check your image path again.",
  "status": 400,
  "response": {
    "error": {
      "message": "Invalid parameter",
      "type": "OAuthException",
      "code": 100,
      "error_subcode": 2490361,
      "is_transient": false,
      "error_user_title": "The provided image file is invalid",
      "error_user_msg": "The provided image file is invalid. Please check your image path again.",
      "fbtrace_id": "AcOHTk6MviAmBmqB5J242i7"
    }
  }
}

原代码

const createAdImage = async (url) => {
  console.log("here");
  account
    .createAdImage([AdImage.Fields.Hash], {
      [AdImage.Fields.filename]: url,
    })
    .then((result) => {
      console.log(result);
      return result.hash;
    })
    .catch((error) => {
      console.log(error);
    });
};

createAdImage("/home/philowe2001/fb-test/AdsSample/philoicon.jpg");

可用的curl请求参考

curl \
  -F 'filename=@/home/philowe2001/fb-test/AdsSample/philoicon.jpg' \
  -F 'access_token=EAAJT1aMW9V0BAGTlre3ZBkkQju5HrQo1zi1CDG9r6sok74PGk7yFynPffGqbGPUZBCNBlYe6EiFNFrc8hoghi4bidCqIVAhx3SNmQEY9JTHc3cb7scgwaok98TXMiJll8o7ZAlWrK1LNBbz0KWpaZByNQmJa2DaAIZAJvXX2Q3aeWo30lbEwdIZCX8KqaEy6EZD' \
  https://graph.facebook.com/v15.0/act_1431237097286304/adimages

解决方案

问题核心在于NodeJS SDK与curl的参数传递逻辑不同:curl中@符号表示上传本地文件,而SDK的createAdImage方法不能直接接收文件路径字符串,需要传递文件的可读流,并使用file字段而非filename字段来传入内容。

修改后的代码示例:

const fs = require('fs');
const createAdImage = async (filePath) => {
  try {
    // 创建目标文件的可读流
    const fileStream = fs.createReadStream(filePath);
    const result = await account.createAdImage(
      [AdImage.Fields.Hash],
      {
        // 用file字段传入文件流,替代原有的filename传路径逻辑
        [AdImage.Fields.file]: fileStream
      }
    );
    console.log(result);
    return result.hash;
  } catch (error) {
    console.log(error);
  }
};

createAdImage("/home/philowe2001/fb-test/AdsSample/philoicon.jpg");

关键说明

  1. 替换参数字段:将filename字段改为file,传入文件可读流而非路径字符串,对齐curl的文件上传逻辑
  2. 优化异步流程:原async函数未使用await,改用try/catch+await的写法,避免异步回调的流程问题
  3. 权限校验:确保Node进程具备读取目标文件的权限,文件路径无拼写错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 16:40:27