facebook - 使用 Facebook 广告管理 API 上传图像

标签 facebook powershell

我正在尝试将图像上传到 Powershell 中的 Facebook 广告管理 API,以便稍后在创建实际广告时使用图像哈希。

$fileName = "adimage.jpg"
$fileContent = get-content $fileName
$fileContentBytes = [System.Text.Encoding]::UTF8.GetBytes($fileContent)
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)

$parameters = @{
    access_token = "abc"
    bytes = $fileContentEncoded
}

$result = Invoke-WebRequest -Uri "https://graph.facebook.com/v2.7/act_12345678/adimages" -Method Post -ContentType "image/jpeg" -body $parameters

我总是得到:

{"error":{"message":"Invalid parameter","type":"OAuthException","code":100,"error_subcode":1487242,
"is_transient":false,"error_user_title":"Image Resize Failed",
"error_user_msg":"Image Resize Failed:Could not get image size","fbtrace_id":"Bl\/fu39rM2W"}}

adimages 端点的 API 页面是: https://developers.facebook.com/docs/marketing-api/reference/ad-image

我们基本上正在寻找相当于

curl -F "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9afa0a5aca7a8a4acf489a8ada0a4a8aeace7a3b9ae" rel="noreferrer noopener nofollow">[email protected]</a>" -F "access_token=abc" https://graph.facebook.com/v2.7/act_12345678/adimages

我也尝试过:

  • 使用 png 图像
  • 使用“bytes = (get-content adimage.jpg -Raw)”
  • 通过 -infile 参数发布图像并将访问 token 添加为参数而不是表单字段
    • 此操作(以及其他一些变体)的结果是,我没有收到任何错误和 200 响应,但内容字段为空,也没有上传任何文件

有问题的图像是: 奥 git _a 和 https://www.dropbox.com/s/hkx236uiiy1p54e/adimage.jpg?dl=0

通过 Assets 管理 GUI 上传有效。

有什么想法吗?

桑德罗

更新:它确实像Mac上的curl一样工作。这使得它更不可能与图像本身有关

最佳答案

我在使用node.js调用Graph API时遇到了同样的问题。我尝试对文件字节进行 UTF-8 编码,然后对结果进行 Base64 编码,但出现了相同的错误。

当我放弃 UTF-8 文件编码并使用原始字节(仍然是 base64 编码)时,它对我来说效果很好。

因此,此操作失败,并出现相同的“图像调整大小失败”错误:

fs.readFile(imagePath, 'UTF8', (err, fileData) => {
    let postData = {
        name: 'My Image',
        bytes: new Buffer(fileData).toString('base64')
    };    
    request.post(url, {form: postData}, (err, response, body) => ...

但这有效(没有 UTF-8 编码):

fs.readFile(imagePath, (err, fileData) => {
    let postData = {
        name: 'My Image',
        bytes: new Buffer(fileData).toString('base64')
    };
    request.post(url, {form: postData}, (err, response, body) => ...

关于facebook - 使用 Facebook 广告管理 API 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39080240/

相关文章:

android - 设置安卓 :value with a gradle properties on manifest

swift - ios swift 2 代表该页面在 facebook 页面上发帖

powershell - 如何使用 PowerShell 替换文件中出现的每个字符串?

powershell - 屏蔽字段重复

powershell - Invoke-Webrequest 在 POST 上得到 401,但在使用基本身份验证的 GET 上却没有得到 401

java - 仅导入 Facebook 好友

ios - 在 iOS 中从 Facebook 获取个人资料照片

android - 为 Android Facebook 应用程序查找哈希键

sql-server - 为什么 SQL SMO 不返回我的 View ?

powershell - While循环检查注册表的值,然后运行exe文件